Explore Numbers and Math
Numbers and Math
Let's explore numbers and math.
Explore:
- Guess the result before running the code.
- Create your own examples.
Numbers vs Strings
Explore the difference between numbers and strings.
// When you have quotes around a number it acts like a string. When you have no quotes around a number the number acts like a number.console.log(typeof "3");console.log(typeof 3);// The + concatenates strings but the same symbol + adds numbers.let resultOfConcatenatingStrings = "Hello" + "everyone!";let resultOfConcatenatingStringsThatLookLikeNumbers = "3" + "24";let resultOfAddingNumbers = 3 + 24;// Guess what the result will look like before printing.console.log(resultOfConcatenatingStrings);console.log(resultOfConcatenatingStringsThatLookLikeNumbers);console.log(resultOfAddingNumbers);
Math Operators
Explore math operations.
// You can do the regular math operationsconsole.log(42+2);console.log(42-2);console.log(42*2);console.log(42/2);// Or even do several math operationsconsole.log(2+3*4);// Note that math operations follow the order of operations// It is common to use parentheses to influence the order of operations or to make it clear what happens firstconsole.log((2+3)*4);console.log(2+(3*4));
Increment & Decrement
Explore increment and decrement.
// There is a special shorthand for adding and subtracting one.let num = 3;console.log(num);num++;console.log(num);num--;console.log(num);num--;console.log(num);
Things you can do with numbers
Explore what you can do with numbers.
// There are some special things you can do with numbers// Example #1 using toFixed// Let's do mathlet totalRent = 3031;let numberOfPeopleInApartment = 3;let averageRentPerPerson = totalRent/numberOfPeopleInApartment;console.log("$" + averageRentPerPerson);// You can use toFixed make a number have a certain number of decimalsconsole.log("$" + averageRentPerPerson.toFixed(2));// Example #2 using toExponential// Let's do mathlet gramsOfWaterMeasuredInASample = 4.31;let molesOfWaterPerGram = 18.02;let molesOfWaterInSample = gramsOfWaterMeasuredInASample/molesOfWaterPerGram;console.log(molesOfWaterInSample);// Now since this is science we want the answer in scientific notation. Let's use toExponential().console.log(molesOfWaterInSample.toExponential());// And your chemistry lab teacher wants the proper significant figures which in this case is three.console.log(molesOfWaterInSample.toExponential(2));
Getting Values from Number and Math
The capital words Number and Math have a special meaning in JavaScript. Let's explore!
// There is the concept of infinityconsole.log(234/0);// You can express infinity with the keyword Number.console.log(Number.POSITIVE_INFINITY);// There also is the concept of negative infinityconsole.log(-234/0);console.log(Number.NEGATIVE_INFINITY);// With the Math keyword you can also express numbers like pi and e.console.log(Math.PI);console.log(Math.E);// Here we can use MATH.PI in lieu of pilet radiusOfACircle = 3;let areaOfACircle = Math.PI * radiusOfACircle * radiusOfACircle;console.log(areaOfACircle);
The Math Keyword to Do Things to Numbers
The Math keyword not only gives you specific numbers like pi, but can also do common math operations.
// You can use the math keyword to do specific math operations// Absolute valueconsole.log(Math.abs(-15));// Roundingconsole.log(Math.round(1.61607));