# Data Types in JavaScript

Every programming language has its own way of representing data.
In javascript, there are 7 data types.

## 1. Number

Numbers in JavaScript are stored as 64-bit floats, and can be manipulated using the built-in operators.

There are a few noteworthy cases for numbers:

• Largest safe integer: The largest possible integer that can be represented with 100% accuracy is given by `Number.MAX_SAFE_INTEGER`, and is equal to 253 - 1, or `9007199254740991`. Numbers are always signed, so `Number.MIN_SAFE_INTEGER` gives the minimum safe integer, which is `-1` times max safe integer.
• Largest value: The largest possible value than can be stored is given by `Number.MAX_VALUE`, and is equal to 21024 - 1. Again, the minimum value is `-1` times the maximum value.
• Smallest value: The smallest value greater than 0 is given by `Number.MIN_VALUE`, and is equal to 2-1074, or `5e-324`.
• Epsilon: The difference between 1 and the least number greater than 1 is given by `Number.EPSILON`, and is 2-52
• Infinity: If a number goes outside the above limit, it will be either `Infinity` or `-Infinity`. We can check for this case using the function `Number.isFinite`. A few common cases involving Infinity are: `1 / 0 == Infinity`, and `1 / Infinity == 0`.
• NaN: `NaN` is anything that is βNot a Numberβ. If you attempt to convert an invalid non-number into a number, you will get `NaN`. Instead of throwing an error, any arithmetic operation with `NaN` will result in `NaN`. We can check if a number is NaN using `Number.isNaN`.

We can create numbers in a few ways:

``````const num1 = 123; // Literal declaration
const num2 = Number(123); // Using the Number constructor
const num3 = Number.parseInt("123.45"); // Conversion to integer
const num4 = Number.parseFloat("1.23e2"); // Conversion to float
const num5 = +"123"; // unary conversion to number
``````

We can also perform some basic built-in operations:

``````const a = 12;
const b = 34;

a + b; // 46

// subtraction
a - b; // -22

// multiplication
a * b; // 408

// division
a / b; // 0.35294117647058826

// exponentiation
b ** a; // 2386420683693101000

// increment
a++; // 13

// decrement
b--; // 33

// modulus (remainder of the division)
b % a; // 10
``````

There are also some binary operations we can use, but be careful, these truncate both terms to signed integers:

``````// bit shift left
34 << 12; // 139264

// bit shift right
257 >> 4; // 16

// unsigned bit shift
-1 >>> 2; // 1073741823

// bitwise or
37 | 12; // 45

// bitwise and
124 & 43; // 40

// bitwise xor
124 ^ 43; // 87

// bitwise not (signed)
~5; // -6
``````

More complicated operations are available through the built-in Math library:

``````Math.sin((Math.PI * 11) / 3) / Math.sqrt(3); // -0.5
``````

## 2. String

Strings are always declared through quotations:

``````const s1 = "this is a string"; // double quotes
const s2 = 'this is also a string'; // single quotes
const s3 = `this is, once more, a string`; // backticks
``````

Single and double quoted strings are identical in JavaScript. Backtick quotes are template literals, allowing for extended functionality seen below:

``````const s4 = ` a
b
c`; // multiline strings

const tmp = 1 + 2;
const s5 = ` 1 + \${1 + 1} = \${tmp} `; // embedded expressions and variables
s5 === " 1 + 2 = 3 "; // true
``````

Any legal expression can be embedded within the braces in backtick strings, including function calls, variables, and even other strings.

Strings can also be concatenated with the addition operator:

``````const s6 = "string1" + " " + "string2"; // "string1 string2"
``````

## 3. Boolean

Booleans are the logical values, either `true` or `false`. They are the result of logical comparisons:

``````const b1 = !false; // true
const b2 = true && false; // false
const b3 = false || true; // true
const b4 = 123 === "456"; // false
const b5 = 1.23 === 123e-2; // true
``````

## 4. Symbol

Symbols are unique values created from string keys. Two Symbols created from the same key are not equal.

``````Symbol("123") === Symbol("456"); // false
Symbol("1") === Symbol("1"); // false

const x = Symbol("abc");
x === x; // true
``````

There is also a Symbol registry which is shared throughout the entire JavaScript process. Symbols you create will not occur in the registry. Instead, you must retrieve them with `Symbol.for`. If you attempt to retrieve a Symbol that does not exist in the registry, one will be added and all subsequent requests for that key will return that Symbol.

``````Symbol("123") === Symbol.for("123"); // false
Symbol.for("123") === Symbol.for("123"); // true
Symbol.for("123") === Symbol.for("456"); // false
``````

Lastly, you can check if a Symbol is from the registry with `Symbol.keyFor`, which will check the registry for a Symbol, and return its key if it exists, and `undefined` otherwise.

``````const mySymbol = Symbol("myKey");
const theirSymbol = Symbol.for("myKey");

mySymbol === theirSymbol; // false
Symbol.keyFor(mySymbol); // undefined
Symbol.keyFor(theirSymbol); // "myKey"
``````

## 5. Object

Objects are collections of key/value pairs, where the keys are strings and the values can be any type, even other objects. Object literals are defined by the list of pairs `key: value`, comma-separated, enclosed by curly braces.

Values can be accessed from objects with the dot operator followed by the key name, or square brackets enclosing the key as a string.

``````const myObject = { first: "123", second: 456, third: true };

myObject.first; // "123"

let second = "first";
// Dot accesses must be literal, not variables
myObject.second; // 456

// bracket accesses can be variables
myObject["first"] === myObject[second]; // true

// You can also write onto objects
myObject.first = 12;
myObject["first"]; // 12
``````

## 6. undefined

`undefined` is a global variable which represents the non-existence of data. When you attempt to access a key on an Object which does not exist, the result will be `undefined`.

``````false === undefined; // false
({ a: "b" }["otherkey"] === undefined); // true
``````

The `void` keyword can be used to ignore the output of an expression. Writing `void x` or `void(x)` will evaluate `x`, and return undefined

``````function f() {}
undefined === void f(); // true, and f gets called
``````

## 7. null

`null` is used to represent the absence of identity, and can only come from a literal expression.

``````let x = null;
``````

`null` is often used to indicate that something may be expected, but is currently unavailable. For example, if a database is represented as an object, and keys are added whenever new data is received, the expected keys could be initialized to `null`. That way if a key is accessed, the user knows that there is nothing yet, but the key was correct.

Be careful when comparing `null` and `undefined`, as loose inequality will hold between them:

``````null == undefined; // true
undefined == null; // true
undefined === null; // false
``````

### π΅ Search Results

π Searching...