# 10+ Types of Data in JavaScript

--

1. Number

Number includes both integers and floats. In JavaScript you don’t need to initialize number variables using int keyword for integers and float / double keyword for floats like in C or other languages. Just initialize with var / let / const and JavaScript will automatically do the calculations for you.

The largest value a Number can hold is about 1.8×10308. Numbers beyond that are replaced with the special Number constant `Infinity`.

A Number only keeps about 17 decimal places of precision;

`8 === 8.0 // true1/0 // Infinity-1/0 === -Infinity // true`

You can declare numbers of other base with 0b(Binary), 0o(Octal) and 0x(Hexadecimal) in the beginning.

`const fifteen = 0b1111;fifteen === 0xF // truefifteen === 15 // true`

Or you can convert a string to number using `parseInt()` .

`parseInt(“1111”, 2); // 15parseInt(“F”, 16); // 15`

2. BigInt

BigInt is a built-in object whose constructor returns a bigint primitive — also called a BigInt value, or sometimes just a BigInt — to represent whole numbers larger than 2²⁵³ — 1 (Number.MAX_SAFE_INTEGER). BigInt values can be used for arbitrarily large integers for Mathematical or other large calculations.

3. NaN

NaN means not a number. There are five different types of operations that return NaN:

· If you try to parse something that cannot be parsed into integers.

`parseInt(“muhahaha”)parseInt(undefined)`

· Mathematical operations where the result is not a real number

`Math.sqrt(-1)`

· Operand of an argument is NaN

`10 ** NaN`

· Indeterminate forms

`0 * Infinity5 * undefined`

· Any operation that involves a string and is not an addition operation

`“blablabla” / 3`

4. String

Strings are plain text data. You can declare a string in 4 different ways:

`const stringUsingDoubleQuote = “A string primitive”;const stringUsingSignleQuote = ‘Also a string primitive’;const templateLiterals = `Yet another string primitive`;const stringUsingConstructor = new String(“A String object”);`

Methods like charAt(), substring(), slice() etc can be used on strings.

5. Boolean

A JavaScript Boolean represents one of two values: true or false.

False, 0, “”, undefined, null etc are considered false. Anything that is not false is true.

`5 > 0 // true5 == true // true“” == true // false[] == true // true`

6. Object

JavaScript objects can be thought of as simple collections of name-value pairs. As such, they are similar to:

· Dictionaries in Python.

· Hashes in Perl and Ruby.

· Hash tables in C and C++.

· HashMaps in Java.

· Associative arrays in PHP.

Functions and arrays are objects too in JS. Also there are some build in objects like Date.

7. Function

To declare a function, use the function keyword.

`function validFunctionName(parameter) {return statement;}`

But not always! You can declare functions in JS without the function keyword too.

`const validFunctionName = (parameter) => {return statement;}`

These are called arrow functions. There are other ways to declare a function too.

8. Array

`let fruits = ['Apple', 'Banana']console.log(fruits.length)// 2`

9. Date

JavaScript `Date` objects represent a single moment in time in a platform-independent format.

JS `Date` object is interesting. It contains a `Number` that represents milliseconds since 1 January 1970 UTC.

New Date().getTime() will return something like 1620284104169 which means 1620284104169 milliseconds has passed from January 1, 1970.

`let today = new Date();let anotherDay = new Date('December 17, 1995 03:24:00')let anotherDay = new Date('1995-12-17T03:24:00')let anotherDay = new Date(1995, 11, 17)            // the month is 0-indexedlet anotherDay = new Date(1995, 11, 17, 3, 24, 0)let anotherDay = new Date(628021800000)            // passing timestamp`

10. RegExp

RegExp means regular expression. Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects.

11. null

The value `null` represents the intentional absence of any object value.

12. undefined

The variables that are declared but not assigned any value is undefined.

--

--

The more you know, the more you realize you know nothing.