Search⌘ K
AI Features

Data Types and Arithmetic Operators

Explore JavaScript primitive data types such as boolean, number, string, null, undefined, bigint, and symbol. Learn how to apply basic arithmetic operators including addition, subtraction, multiplication, division, modulus, and exponentiation. Understand floating point behaviors, the concept of NaN, and how JavaScript handles infinity values.

Introduction

Most programming languages help you create values that symbolize a number, a character in a text, or a longer text. You can also symbolize the concept of true and false values using booleans. You can also create values that symbolize the absence of a value. These values are all called primitive datatypes.

The name primitive does not come from a negative place. These datatypes are not inferior to any other data types we use in JavaScript. The name primitive comes from their simplicity. The other datatypes you will learn later are composite, and they consist of many primitive data types.

Primitive types

In JavaScript, there are seven primitive types:

  1. boolean (true or false)
  2. number (including integers like 1, -2, and floating point numbers like 1.1, 2e-3)
  3. string ( ‘’ or “” e.g. ‘ES6 in Practice’ or “ES6 in Practice” )
  4. null type (denoted by null)
  5. undefined (denoted by undefined)
  6. bigint (to represent big integers)
  7. Symbol (don’t worry about them yet)

Integers and arithmetic operators

Let’s try some JavaScript expressions.

Node.js
console.log(5+2);
console.log(7%5);
console.log(5**2);

Handling integers is straightforward. You have the four arithmetic operations ( +, -, *, /) available for addition, subtraction, multiplication, and division, respectively.

The % operator is called modulus. a % b returns the remainder of the division a / b. In our example, 7 / 5 is 1, and the remainder is 2. The value 2 is returned.

The ** operator is called the exponential operator. 5 ** 2 is five raised to the second power.

Floating numbers

Let’s run the code below.

Node.js
console.log(0.1+0.2);
console.log(3.1e-3);

Due to the way how numbers are represented, 0.1 + 0.2 is not exactly 0.3. This is normal and occurs in most programming languages.

3.1e-3 is the normal form of 0.0031. Read it like the exact value of 3.1 times ten to the power of minus three. Although the form is similar to 3.1 * (10 ** -3), there are subtle differences. 3.1e-3 describes the exact value of 0.0031.
3.1 * (10 ** -3) describes a composite expression that needs to be calculated:

Node.js
console.log(3.1 * (10 ** -3))

Even floating point arithmetics does not make it exact.

NaN

The division 0 / 0 or using mismatching types creates a special number called not a number or NaN.

Node.js
console.log(0 / 0);
console.log('Javascript in Practice'*2);

The latter in the code example above is interesting to Python users because, in Python, the result would have been ‘Javascript in PracticeJavascript in Practice’. JavaScript does not work like that.

Infinity type

There is another interesting type: infinity.

Node.js
console.log(1 / 0);
console.log(Infinity * Infinity);
console.log(-1 / 0);
console.log(1e+308);
console.log(1e+309);

JavaScript registers very large numbers as infinity. For instance, ten to the power of 309 is represented as infinity. Division by zero also yields infinity.

Data Types and Arithmetic Operators

1.

Does JavaScript offer any primitive type for strings?

A.

Yes

B.

No


1 / 2