{"id":19767,"date":"2020-07-17T19:58:18","date_gmt":"2020-07-18T02:58:18","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19767"},"modified":"2020-12-29T13:44:15","modified_gmt":"2020-12-29T21:44:15","slug":"javascript-math-abs","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/","title":{"rendered":"JavaScript Math Object: Absolute Value"},"content":{"rendered":"\n<p><em>This article will show you how to use the Math Object to figure out the absolute value of an input.&nbsp;<\/em><br><\/p>\n\n\n\n<p>As a reminder, the absolute value of a number disregards the sign \u2013 it assumes that all numbers are 0 or greater and returns that number. To use the absolute value method of the Math Object, here\u2019s the syntax:<br><\/p>\n\n\n\n<p><code>Math.abs(inputVal);<br><\/code><\/p>\n\n\n\n<p>Since we are using the Math Object, we start with Math. The absolute value method is the abbreviation abs. The set of parentheses invokes the method on the input value inside the parentheses and returns the absolute value.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n &lt;head&gt;\n   &lt;meta charset=&quot;utf-8&quot;&gt;\n   &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot;&gt;\n   &lt;title&gt;repl.it&lt;\/title&gt;\n   &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; \/&gt;\n &lt;\/head&gt;\n &lt;body&gt;\n   &lt;form onsubmit=handleSubmit(event)&gt;\n     &lt;label for=&quot;absolute-value&quot;&gt;Enter a number:&lt;\/label&gt;\n     &lt;input id=&quot;absolute-value&quot; onchange=handleChange(event) type=&quot;text&quot; name=&quot;absolute-value&quot; value=&quot;&quot;\/&gt;\n     &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; \/&gt;\n   &lt;\/form&gt;\n \n   &lt;h3 id=&quot;root&quot;&gt;&lt;\/h3&gt;\n \n   &lt;script&gt;\n     let inputVal = &quot;&quot;\n \n     const handleChange = e =&gt; {\n \n       inputVal = e.target.value;\n       console.log(inputVal)\n     }\n     const handleSubmit = e =&gt; {\n       e.preventDefault();\n       const root = document.querySelector(&quot;#root&quot;);\n       root.innerHTML = Math.abs(inputVal);\n \n     }\n     const inputValue = document.getElementById(&quot;absolute-value&quot;).value\n   &lt;\/script&gt;\n &lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>If you were to enter in a string, it returns NaN. A null value would return 0, and positive or negative numbers would, of course, return the positive number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s it! You can now use the Math object to figure out the absolute value of a number. Here\u2019s some articles that can help you figure out what to learn next:&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-random-number\/\">JavaScript Random Number: A Complete Guide<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-parseint\/\">JavaScript ParseInt: A Step-By-Step Guide<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-countdown-timer\/\">JavaScript Countdown Timer: A Tutorial<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-tostring\/\">JavaScript toString<\/a><\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/javascript-touppercase-tolowercase\/\">JavaScript toUpperCase and toLowerCase<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"This article will show you how to use the Math Object to figure out the absolute value of an input.&nbsp; As a reminder, the absolute value of a number disregards the sign \u2013 it assumes that all numbers are 0 or greater and returns that number. To use the absolute value method of the Math&hellip;","protected":false},"author":77,"featured_media":19768,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-19767","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>JavaScript Math Object: Absolute Value | Career Karma<\/title>\n<meta name=\"description\" content=\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Math Object: Absolute Value\" \/>\n<meta property=\"og:description\" content=\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-18T02:58:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T21:44:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"666\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"JavaScript Math Object: Absolute Value\",\"datePublished\":\"2020-07-18T02:58:18+00:00\",\"dateModified\":\"2020-12-29T21:44:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/\"},\"wordCount\":188,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/blue-and-white.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/\",\"name\":\"JavaScript Math Object: Absolute Value | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/blue-and-white.jpg\",\"datePublished\":\"2020-07-18T02:58:18+00:00\",\"dateModified\":\"2020-12-29T21:44:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/blue-and-white.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/blue-and-white.jpg\",\"width\":1000,\"height\":666},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-math-abs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Math Object: Absolute Value\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\\\/\\\/www.linkedin.com\\\/in\\\/cmvnk\"],\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/christina-kopecky\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JavaScript Math Object: Absolute Value | Career Karma","description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Math Object: Absolute Value","og_description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.","og_url":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-18T02:58:18+00:00","article_modified_time":"2020-12-29T21:44:15+00:00","og_image":[{"width":1000,"height":666,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"JavaScript Math Object: Absolute Value","datePublished":"2020-07-18T02:58:18+00:00","dateModified":"2020-12-29T21:44:15+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/"},"wordCount":188,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/","url":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/","name":"JavaScript Math Object: Absolute Value | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg","datePublished":"2020-07-18T02:58:18+00:00","dateModified":"2020-12-29T21:44:15+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"The JavaScript Math object has several methods to choose from to use in your logic. One such method is the absolute value method. In this article, we\u2019ll learn how to use the Math.abs() method with examples.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/javascript-math-abs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/blue-and-white.jpg","width":1000,"height":666},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/javascript-math-abs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Math Object: Absolute Value"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19767"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19767\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19768"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}