02. Numbers

Declaration

There are several ways to declare a number in JavaScript

  • Integer: 5
  • Floating point numbers: 3.1415
  • Scientific notation: 9e+4
  • Hexadecimal notation: 0xfff

Operators

Furthermore, there are a handful of common arithmetic operators available in JavaScript.

+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo
++
Increment
--
Decrement

Conversion from strings

To convert a number to a string, you can either use the parseInt() or parseFloat() method, depending on whether you want to truncate the decimal point.

parseInt('22') // 22
parseFloat('3.14'); // 3.14
parseFloat('23.4 hahahahahlololol'); // 23.4

In the last example, only the first number in the string is parsed. If there are no values at the beginning, you'll get a NaN data type.

Number methods

These methods can be used with any number value.

toFixed(x)
Rounds to a specified number of decimal places.
toPrecision(x)
Formats number to x length.
toExponential(x)
Returns a string representation of the number in exponential notation.
toString()
Converts a number to a string.
MAX_VALUE
Returns the largest number possible.
MIN_VALUE
Returns the smallest number possible.
NEGATIVE_INFINITY
Represents negative infinity.
POSITIVE_INFINITY
Represents infinity.
var sampleNum = 5;
sampleNum.toPrecision(4);    // 4.000
sampleNum.MAX_VALUE;         // 1.7976931348623157e+308

The Math object

The Math object contains a library of parameters and functions related to number generation and manipulation. To call it, call the Math variable, and either its attributes or a function.

Here are some common constants that are stored as attributes in the Math object:

Math.PI
Returns PI (3.14).
Math.E
Returns Euler's number.
Math.LN2
Returns natural logarithm of 2.

Furthermore, the Math object contains a number of functions for complex calculations.

Math.abs(x)
Returns absolute value of x.
Math.floor(x)
Returns floor of x.
Math.max(x, y, z)
Returns max value of x, y or z.
Math.min(x, y, z)
Returns min value of x, y or z.
Math.round(x)
Returns nearest integer value of x.
Math.sin(x)
Returns sine of x.
Math.sqrt(x)
Returns square root of x.
Math.isNaN(x)
Returns true if x is not a number.
Math.min(1, 4, 5);  // 1
Math.floor(1.242);  // 1
Math.isNaN(21);     // false

We'll soon cover objects, but for now just use this notation to access Math's attributes and functions.

Generating random numbers

To generate random numbers, use the Math.random() function. This will return a value from 0 up to 1.

Thus we can get a random number between 1 and 100 with the following code:

var randomNum;
randomNum = Math.floor((Math.random() * 100) + 1);

Dot notation

The dot notation is called so because it uses the period (or the dot) to access an object's methods and properties. As you will (or have) seen in many other programming languages, the dot notation provides a clean way to access the members of an object.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources

Ad