Values, Types, and Operators in JavaScript

Suhadev Venkatesh
3 min readMay 28, 2019

--

In the Computer’s world, Data is stored as a sequence of bits. Bits are basically either 0 or 1. Any data inside a computer is expressed in terms of bits. For example, the decimal number 13 will be expressed as 00001101 in bits inside the computer.

Imagine a sea of bits. In order to work with such huge quantities of bits without getting lost, those bits are separated into chunks to represent some information and these chunks are called Values. There are different types of values called Numbers, String, Boolean, etc. Let’s see one by one in detail.

A typical modern computer has around 30 billion bits in its volatile data storage alone.

Numbers:

Values of number type are just numerical values. Javascript uses 64 bits to store a single number value. With 64 binary digits, we can represent 2 ^ 64 different numbers which is 18 quintillion and its so huge. Number values include negative numbers as well, so one bit is used to represent the sign of the number. In order to represent nonwhole numbers as well, some of the bits are used to store the decimal point.

With Numbers, all we can do is Arithmetic operations like addition, subtraction, etc. A sample arithmetic operation in Javascript looks like this

10 + 5 * 2

The symbols +, * are called operators. When operators appear without parenthesis just like the expression on the top, the arithmetic operation is performed on the basis of the order of precedence.

There are three special values in Javascript which are numbers but doesn’t behave like one. They are Infinity, -Infinity, NaN.

Strings:

Strings are used to represent text. They are enclosed within quotes. Strings can be represented within single or double quotes or within backticks.

“Javascript” ‘Phython’ ` R`

Strings also have to be modeled as a sequence of bits inside a computer. Javascript does this based on Unicode standard which assigns a number to each and every character which means Strings will be modeled as a sequence of numbers. Using + operator on string concatenate the strings together. The below expression will produce the string Concatenate.

“Con” + “cat” + “enate”

Backtick-quoted strings also know as template literals can also embed other values. When we write something between ${}, its result will be computed, converted to a string. The output of the below expression would be “The sum of 2 and 3 is 5”.

` The Sum of 2 and 3 is ${2 + 3}`

Boolean Values:

Boolean values are either true or false. Boolean values can even be generated using comparison operators like>, <, etc.

console.log(3 > 2)

The above expression will return true. There is only one value in Javascript which is not equal to itself. Console.log(NaN == NaN) will return false.

Logical Operators:

Javascript supports three logical operators &&, ||, ! which can be used to reason about boolean. The && operator represents logical and. It returns true only if both the values are true.

Console.log(true && false) will return false.

The || operator represents logical OR. It returns true if either of the values given to it is true.

console.log(true || false) will return true

! is a unary operator which flips its input value to the opposite. ! true will result in false.

The conditional operator ?: is a ternary operator which takes three operands.

let result = condition ? value1 : value2

The condition before ? is evaluated and based on the results, if the condition is true, value 1 is assigned to result and if it is false, value 2 is assigned.

Empty Values:

There are two special values null and undefined which are used to denote the absence of meaningful value.

Reference:

Eloquent JavaScript by Marijn Haverbeke

www.Javascript.info

--

--