Learn these JavaScript fundamentals and become a better developer

Primitives, variables, objects, arrays, dynamic typing, and more

Cristian Salcescu
Frontend Essentials

--

Photo by the author

JavaScript has primitives, objects, and functions. All of them are values. All are treated as objects, even primitives.

Primitives

Number, boolean, string, undefined and null are primitives.

Number

There is only one number type in JavaScript, the 64-bit binary floating-point type. Decimal numbers’ arithmetic is inexact.

As you may already know, 0.1 + 0.2 does not make 0.3 . But with integers, the arithmetic is exact, so 1+2 === 3 .

Numbers inherit methods from the Number.prototype object. Methods can be called on numbers:

(123).toString();  //"123"
(1.23).toFixed(1); //"1.2"

There are functions for converting strings to numbers : Number.parseInt(), Number.parseFloat() and Number():

Number.parseInt("1")       //1
Number.parseInt("text") //NaN
Number.parseFloat("1.234") //1.234
Number("1") //1
Number("1.234") //1.234

Invalid arithmetic operations or invalid conversions will not throw an exception but will result in the NaN “Not-a-Number”…

--

--