{"id":21844,"date":"2018-06-08T12:15:58","date_gmt":"2018-06-08T09:15:58","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=21844"},"modified":"2018-06-08T12:18:32","modified_gmt":"2018-06-08T09:18:32","slug":"mathematics-with-mathjs","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/","title":{"rendered":"Mathematics with MathJS"},"content":{"rendered":"<p>One of the language design decisions in JavaScript has followed in C\u2019s footsteps by providing implicit type conversions based on what operators you give it. For example in JavaScript, you can add <code>true + true<\/code> to get <code>2<\/code>. While it may be convenient in some cases, this has been a pain point for many developers trying to locate and remedy bugs in their programs and makes it more difficult for developers new to the language to get their desired result. So to save yourself from the headache of potential unintended type coercion with mathematics, you can be explicit with using MathJS and better guarantee the results you want.<\/p>\n<p>MathJS is a very robust mathematics library that provides the capability to perform calculations in many additional math categories. It also provides unit conversion with minimal setup for custom unit types.<\/p>\n<p>MathJS is the Swiss Army knife for having peace of mind when working with numbers, conversions, and mathematics in JavaScript.<\/p>\n<h2>Setting Up TDD Environment for Trying MathJS<\/h2>\n<p>I wanted to find an enjoyable way to test out most any JavaScript library from the comfort of my computer console. And here\u2019s an elegant and easy way to get started \u2014 you\u2019ll need to have <a href=\"https:\/\/nodejs.org\/en\/\">NodeJS<\/a> and <a href=\"https:\/\/yarnpkg.com\/lang\/en\/docs\/install\/\">Yarn<\/a> installed on your machine.<\/p>\n<p><a href=\"https:\/\/www.npmjs.com\/\">npm<\/a> is an alternative to Yarn you may use, but I won\u2019t be giving instructions for that.<\/p>\n<p>For testing, we\u2019ll use <a href=\"https:\/\/mochajs.org\/\">Mocha<\/a> and <a href=\"http:\/\/www.chaijs.com\/\">Chai<\/a>, and for elegance, we\u2019ll be writing our code in <a href=\"https:\/\/coffeescript.org\/\">CoffeeScript<\/a>.<\/p>\n<p>To ensure that our tests are able to run from global commands on our system, we\u2019ll need to run the following commands:<\/p>\n<pre class=\"brush:bash\">yarn global add mocha\r\nyarn global add coffee-script<\/pre>\n<p>Next we\u2019ll create our project directory and initialize a project with <code>yarn<\/code>.<\/p>\n<pre class=\"brush:bash\">mkdir MathJS\r\ncd MathJS\r\nyarn init<\/pre>\n<p>Now add Mocha and Chai to the project\u2019s development dependencies and add MathJS as a primary dependency:<\/p>\n<pre class=\"brush:bash\">yarn add mocha chai --dev\r\nyarn add mathjs\r\nyarn install<\/pre>\n<p>Now make the folders for the project.<\/p>\n<pre class=\"brush:bash\">kdir bin src test<\/pre>\n<p>And we\u2019ll create files for testing with Mocha. We\u2019ll first put defaults for Mocha in <code>test\/mocha.opts<\/code>.<\/p>\n<pre class=\"brush:bash\">--reporter spec\r\n--require coffee-script\/register<\/pre>\n<p>Then we\u2019ll create a shorthand for executing the tests by making a file <code>bin\/test<\/code>.<\/p>\n<pre class=\"brush:bash\">#!\/bin\/bash\r\nmocha \"test\/**\/*Test.{js,coffee}\"<\/pre>\n<p>Change it to an executable with <code>chmod +x bin\/test<\/code>, and now you can type <code>bin\/test<\/code> on the command line whenever you want to run your tests.<\/p>\n<p>An alternative way to simplify running tests is to use the <code>cake<\/code> executable that is provided globally when we installed CoffeeScript globally. This works like Ruby\u2019s <code>rake<\/code> command; similarly you need to create a <code>Cakefile<\/code> to define the different executable tasks you want defined. It\u2019s a nice system, and I do recommend learning it if you\u2019d like.<\/p>\n<p>Now if you\u2019re familiar with having your tests run automatically whenever a change is committed to a file, you can do that here as well. <code>mocha<\/code> has some command line flag options for watching files. You would basically need to tack on <code>--watch --watch-extensions js,coffee<\/code>.<\/p>\n<p>The problem with this system though is that it remembers everything between each run, so both of your sequential test runs have conflicts with the code already loaded. So you need to be a bit more creative in getting this feature enabled.<\/p>\n<p>For anyone on Ubuntu or using an Ubuntu shell in Windows, Mac, or other platform, you can use the <code>inotifywait<\/code> command to implement something. Here\u2019s what I\u2019ve written that may work for you.<\/p>\n<pre class=\"brush:bash\">#!\/bin\/bash\r\n\r\ntest_watch() {\r\n  clear\r\n  inotifywait --quiet --recursive --monitor --format \"Changed: %w%f\" \\\r\n    --event close_write --exclude '(\\.sw|~|[[:digit:]]+|node_modules)' \\\r\n    . | bin\/test;\r\n  test_watch\r\n}\r\n\r\ntest_watch<\/pre>\n<p>You can save that in <code>bin\/watch<\/code> or make an equivalent entry in your <code>Cakefile<\/code> and be sure to change <code>bin\/watch<\/code> to executable with <code>chmod<br \/>\n+x<\/code>.<\/p>\n<p>The <code>inotifywait<\/code> can only take one exclude flag. You may change the regex matchers to better fit your personal needs.<\/p>\n<p>With that, you can split your terminal editor window with something like tmux, Vim, or the terminal program itself and watch live change results appear in one window while you code in another.<\/p>\n<p>For testing, we\u2019re going to have a default test helper file to load default code to use in all our test files. Create the file <code>test\/testHelper.coffee<\/code> and putting the following line of code in it:<\/p>\n<pre class=\"brush:bash\">global.expect = require('chai').expect<\/pre>\n<p>Likewise in the src directory we\u2019re going to do the same. Create the file <code>src\/app.coffee<\/code> and put the following line in it.<\/p>\n<pre class=\"brush:bash\">global.MathJS = require('mathjs')<\/pre>\n<h2>TDD with MathJS<\/h2>\n<p>First we\u2019ll create two files, one for source and one for testing.<\/p>\n<pre class=\"brush:bash\">touch src\/basicMath.coffee\r\ntouch test\/basicMathTest.coffee<\/pre>\n<p>Then we\u2019ll open up <code>test\/basicMathTest.coffee<\/code> and write our first failing test.<\/p>\n<pre class=\"brush:bash\">require '.\/testHelper'\r\nrequire '..\/src\/basicMath'\r\n\r\ndescribe 'Math basics', -&gt;\r\n  it 'input of 1 and 1 should be 2', -&gt;\r\n    expect(add 1, \"1\").to.equal 2<\/pre>\n<p>This test is testing the function <code>add<\/code>, which we have not defined yet. The parameters have one number as a string, as this is a common input we end up with when calculating things in JavaScript. Now you can go ahead and run your test with <code>bin\/test<\/code> and see your first failing message.<\/p>\n<pre class=\"brush:bash\">Math basics\r\n  1) input of 1 and 1 should be 2\r\n\r\n0 passing (21ms)\r\n1 failing\r\n\r\n1) Math basics\r\n     input of 1 and 1 should be 2:\r\n   ReferenceError: add is not defined\r\n    at Context.&lt;anonymous&gt; (test\/basicMathTest.coffee:8:7)&lt;\/anonymous&gt;<\/pre>\n<p>The next step for TDD is to work toward green. Now we\u2019ll define an add method to our source code in <code>src\/basicMath.coffee<\/code>. We\u2019ll give it the generic JavaScript adding operator for this next test.<\/p>\n<pre class=\"brush:bash\">require '.\/app'\r\n\r\nadd = (a, b) -&gt;\r\n  a + b\r\n\r\nglobal.add = add<\/pre>\n<p>The <code>global.add = add<\/code> exports the <code>add<\/code> function to be globally available. Now we run <code>bin\/test<\/code> to see our new error message.<\/p>\n<pre class=\"brush:bash\">Math basics\r\n  1) input of 1 and 1 should be 2\r\n\r\n\r\n0 passing (24ms)\r\n1 failing\r\n\r\n1) Math basics\r\n     input of 1 and 1 should be 2:\r\n   AssertionError: expected '11' to equal 2\r\n    at Context.&lt;anonymous&gt; (test\/basicMathTest.coffee:6:27)&lt;\/anonymous&gt;<\/pre>\n<p>Here we now see that the <code>add<\/code> method is correctly available to call, but the error message is showing us that when we added <code>1 + \"1\"<\/code> we got a string of <code>11<\/code>. This is part of the coercion behavior that JavaScript has for these types when adding. Now let\u2019s change it to use MathJS and see our results.<\/p>\n<pre class=\"brush:bash\">add = (a, b) -&gt;\r\n  MathJS.add(a, b)<\/pre>\n<p>Run our tests and we get:<\/p>\n<pre class=\" brush:bash\">Math basics\r\n  \u2713 input of 1 and 1 should be 2\r\n\r\n\r\n1 passing (33ms)<\/pre>\n<p>Now this example of using <code>MathJS.add<\/code> is a contrived example but does demonstrate that MathJS has cleared up our worries of unwanted type coercion. If we really wanted to make <code>MathJS.add<\/code> globally available as add, we simply could have done <code>global.add = MathJS.add<\/code>. This was merely to demonstrate code structure for writing and using code with TDD.<\/p>\n<p>Now on to MathJS specifics.<\/p>\n<h2>MathJS<\/h2>\n<p>MathJS is pretty exhaustive in the amount of features it contains, and I will only be highlighting some aspects as they are very well documented. But I feel it is important nonetheless to share and raise awareness of what you can do with MathJS.<\/p>\n<pre class=\"brush:bash\"># You can simplify mathmatical expressions with\r\nMathJS.simplify('x * y * -x \/ (x ^ 2)').toString()\r\n# =&gt; '-y'\r\n\r\n# Find the symbolic derivative of an expression\r\nMathJS.derivative('2x^2 + 3x + 4', 'x').toString()\r\n# =&gt; '4 * x + 3'\r\n\r\n# Evaluate expressions\r\nMathJS.eval('2 inch to cm')\r\n# =&gt; 5.08 cm<\/pre>\n<p>You can chain your method calls together.<\/p>\n<pre class=\"brush:bash\">MathJS.chain(5).multiply(5).add(4).done()\r\n# =&gt; 29<\/pre>\n<p>You may define your own unit measurements and override existing ones.<\/p>\n<pre class=\"brush:bash\">math.createUnit('mile', '1609.347218694', {override: true}})<\/pre>\n<p>You can choose different types of the number you return, like having a fraction be the return type.<\/p>\n<pre class=\"brush:bash\">math.config({ number: 'Fraction' })<\/pre>\n<p>These are just a small glimpse into what you may do with MathJS.<\/p>\n<h3>Some things to keep in mind with MathJS<\/h3>\n<p>When you want to perform division with MathJS, it\u2019s better to use the fraction methods included with it rather than divide. MathJS has included an entirely separate library just for dealing with fractions, and it\u2019s much more accurate in producing desired results.<\/p>\n<p>Also, when you\u2019re writing methods that return values after a calculation, there is a MathJS object that\u2019s generally returned from many of the methods; you may want to simplify that back down to a number by passing it to <code>MathJS.number<\/code>.<\/p>\n<p>When writing a function that needs to perform division and return a number with something like <code>1250 - ((450-98)*37)\/5000<\/code> where each of those numbers could be variables or parameters, it would look like:<\/p>\n<pre class=\"brush:bash\">MathJS.number(\r\n  MathJS.subtract(\r\n    1250\r\n    MathJS.fraction(\r\n      MathJS.chain(450).subtract(98).multiply(37).done()\r\n      5000\r\n    )\r\n  )\r\n)<\/pre>\n<p>This produces a good decimal number result.<\/p>\n<p>When trying to deal with some quirks, like round-off errors in JavaScript where you add <code>0.1 + 0.2<\/code> and get <code>0.30000000000000004<\/code>, even with <code>MathJS.add<\/code> you need to either format it with precision, use <code>Fractions<\/code>, or use <code>BigNumbers<\/code>.<\/p>\n<p>To format it with precision, you would do the following.<\/p>\n<pre class=\"brush:bash\">number = MathJS.add(0.1, 0.2)\r\nMathJS.format(number, {precision: 14})\r\n# =&gt; '0.3'<\/pre>\n<p>Note that the format method will return a string type. As long as you continue using MathJS for further calculations on the returned value, there shouldn\u2019t be an issue. Otherwise pass it to <code>MathJS.number<\/code> to convert the string to an integer or float.<\/p>\n<h2>Counting to Change<\/h2>\n<p>In an example where you would like to calculate the exact coin change from an amount, you could use TDD to invent the process one step at a time. With MathJS\u2019s unit conversion feature, you can completely skip writing the implementation and simply define quantity relations of one coin to another.<\/p>\n<p>So a penny will be the smallest unit of 1, and everything else can relate to that. Using the <code>MathJS.createUnit<\/code> method, we can define all the relations in one go.<\/p>\n<pre class=\"brush:bash\">MathJS.createUnit({\r\n  'penny':\r\n    aliases: ['pennies']\r\n  'nickel':\r\n    definition: '5 pennies'\r\n    aliases: ['nickels']\r\n  'dime':\r\n    definition: '2 nickels'\r\n    aliases: ['dimes']\r\n  'quarter':\r\n    definition: '5 nickels'\r\n    aliases: ['quarters']\r\n  'halfdollar':\r\n    definition: '2 quarters'\r\n    aliases: ['halfdollars']\r\n  'dollar':\r\n    definition: '4 quarters'\r\n    aliases: ['dollars']\r\n})<\/pre>\n<p>With this, we have everything we need to convert a dollar and change amount into the proper coinage. Here\u2019s how that looks after the TDD work as demonstrated above \u2014 the <code>src\/coinCounter.coffee<\/code> file looks like:<\/p>\n<pre class=\"brush:bash\">require '.\/app'\r\n\r\n#\r\n#  The MathJS.createUnit code shown\r\n#  above goes here\r\n#\r\n\r\ndefaultDenoms = -&gt; [\r\n  'dollars'\r\n  'halfdollars'\r\n  'quarters'\r\n  'dimes'\r\n  'nickels'\r\n  'pennies'\r\n]\r\n\r\ncount_change = (amount, denominations) -&gt;\r\n  denoms = denominations ? defaultDenoms()\r\n  MathJS.\r\n    unit(amount).\r\n    splitUnit(denoms).\r\n    toString()\r\n\r\nglobal.count_change = count_change<\/pre>\n<p>And the tests in <code>test\/coinCounterTest.coffee<\/code> to show splitting into coins:<\/p>\n<pre class=\"brush:bash\">require '.\/testHelper'\r\nrequire '..\/src\/coinCounter'\r\n\r\ndescribe 'Makes change', -&gt;\r\n  it 'should produce default denominations of change', -&gt;\r\n    expect(\r\n      count_change('831 pennies')\r\n    ) . to . equal '\r\n      8 dollars,\\\r\n      0 halfdollars,\\\r\n      1 quarters,\\\r\n      0 dimes,\\\r\n      1 nickels,\\\r\n      1 pennies\r\n    '\r\n\r\n  it 'can handle specific small change only', -&gt;\r\n    denominations = ['dimes', 'nickels', 'pennies']\r\n\r\n    expect(\r\n      count_change('831 pennies', denominations)\r\n    ) . to . equal '\r\n      83 dimes,\\\r\n      0 nickels,\\\r\n      1 pennies\r\n    '<\/pre>\n<p>And with running <code>bin\/test<\/code>, we get all tests passing green.<\/p>\n<p>We didn\u2019t need to do any conversion logic of our own \u2014 we simply let MathJS take the unit comparisons we gave it and do the work for us.<\/p>\n<p>My only nitpick about the results is that whenever a value of <code>1<\/code> is returned for any denomination, it would be really nice if it used the singular word for it. But that\u2019s easy enough to program for if it\u2019s a requirement.<\/p>\n<h2>Summary<\/h2>\n<p>Not only does MathJS make calculations safer to do in JavaScript, but it gives us so much that we can do with the robustness of its library. Now we can do finance, Latex, unit conversions, algebra, and much more with the well designed library MathJS. Enjoy!<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Web Code Geeks with permission by Daniel P. Clark, partner at our <a href=\"\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/blog.codeship.com\/mathematics-with-mathjs\/\" target=\"_blank\" rel=\"noopener\">Mathematics with MathJS<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>One of the language design decisions in JavaScript has followed in C\u2019s footsteps by providing implicit type conversions based on what operators you give it. For example in JavaScript, you can add true + true to get 2. While it may be convenient in some cases, this has been a pain point for many developers &hellip;<\/p>\n","protected":false},"author":146,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-21844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Mathematics with MathJS - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"&quot;Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!&quot;\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mathematics with MathJS - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"&quot;Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-08T09:15:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-08T09:18:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Daniel P. Clark\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Daniel P. Clark\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\"},\"author\":{\"name\":\"Daniel P. Clark\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e\"},\"headline\":\"Mathematics with MathJS\",\"datePublished\":\"2018-06-08T09:15:58+00:00\",\"dateModified\":\"2018-06-08T09:18:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\"},\"wordCount\":1507,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\",\"name\":\"Mathematics with MathJS - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2018-06-08T09:15:58+00:00\",\"dateModified\":\"2018-06-08T09:18:32+00:00\",\"description\":\"\\\"Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!\\\"\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Mathematics with MathJS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e\",\"name\":\"Daniel P. Clark\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g\",\"caption\":\"Daniel P. Clark\"},\"description\":\"Daniel P. Clark is a freelance developer, as well as a Ruby and Rust enthusiast. He writes about Ruby on his personal site.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/daniel-clark\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mathematics with MathJS - Web Code Geeks - 2026","description":"\"Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!\"","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:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/","og_locale":"en_US","og_type":"article","og_title":"Mathematics with MathJS - Web Code Geeks - 2026","og_description":"\"Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!\"","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-06-08T09:15:58+00:00","article_modified_time":"2018-06-08T09:18:32+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Daniel P. Clark","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Daniel P. Clark","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/"},"author":{"name":"Daniel P. Clark","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e"},"headline":"Mathematics with MathJS","datePublished":"2018-06-08T09:15:58+00:00","dateModified":"2018-06-08T09:18:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/"},"wordCount":1507,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/","name":"Mathematics with MathJS - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2018-06-08T09:15:58+00:00","dateModified":"2018-06-08T09:18:32+00:00","description":"\"Interested to learn more about MathJS? Then check out our article on Mathematics with MathJS and save yourself from the headache of potential unintended type coercion with mathematics!\"","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/mathematics-with-mathjs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Mathematics with MathJS"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/a79c83c57cba5d8a6e46462149890b5e","name":"Daniel P. Clark","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2342ad14824a1064823dcec81641a12136a3492abae522e473ec723cbfb84bce?s=96&d=mm&r=g","caption":"Daniel P. Clark"},"description":"Daniel P. Clark is a freelance developer, as well as a Ruby and Rust enthusiast. He writes about Ruby on his personal site.","url":"https:\/\/www.webcodegeeks.com\/author\/daniel-clark\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/21844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/146"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=21844"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/21844\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=21844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=21844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=21844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}