A data type describes a set of values and the operations possible on those values.
Primitive
The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
Number
In programming, generally, numbers are categorized into two groups — integers and floats.
Ex:
1 | let num = 2; // Integer |
1 | let num2 = 1.3; // Floating point number |
However, JavaScript doesn’t make any distinction between integers or floats whatsoever — we’ve got only one type for numbers in the language and that is the type ‘number‘.
Ex:
1 2 3 4 5 6 7 8 9 | Console: >> 4 + 6 10 >> 4.6 + 3 7.6 >> 2 - 5 -3 >> 2.5 - 1 1.5 |
All the numbers shown above are known as number literals.
Note:Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity, -Infinity and NaN.
- Infinity represents the mathematical Infinity ∞.
- NaN represents a computational error.
Strings
''
), or a pair of double quotes (""
), or a pair of backticks (
)Ex:
1 2 3 | var lang = 'JavaScript'; var os = "Windows"; var phrase = `can embed ${str}`; |
Double and single quotes are “simple” quotes.
Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in
${…}
.Booleans
In JavaScript, a Boolean is denoted using the literal values true and false.
Ex:
1 2 | var proceed = true; var stopNow = false; |
NULL
1 | let age = null; |
Underfined
1 2 | let x; console.log(x); // undefined |
Symbol
1 2 3 4 5 6 | let symbol1 = Symbol("Geeks") let symbol2 = Symbol("Geeks") // Each time Symbol() method // is used to create new global Symbol console.log(symbol1 == symbol2); // False |
BigInt
A BigInt value is created by appending n
to the end of an integer:
1 2 | // the "n" at the end means it's a BigInt const bigInt = 1234567890123456789012345678901234567890n; |
Object data types
Arrays
Note:Strings and arrays are both sequences of data. Hence the same concepts of indexes and length apply to both of them.
1 | var nums = [1, 5, 10]; |
Functions
A function is used to group together some code that could be executed later on at will. This execution is initiated by calling — or better to say, invoking — the function.
The function
keyword is used to create a function in JavaScript.
Syntax:
statements
}
1 2 3 4 5 6 | function sayHello() { var name = prompt('Enter your name:'); document.write('Hello, ' + name + '.'); } sayHello(); // call the function |
Objects
In this section, we are concerned with the actual object data type in JavaScript that denotes the purest of all objects.
Here’s the syntax of an object literal:
{key1: value1, key2: value2, …, keyN: valueN}
or
{
key1: value1,
key2: value2,
…,
keyN: valueN
}
1 | var obj = {x: 10, y: 20}; |
1 2 3 4 5 | var url = { protocol: 'https', domain: 'www.codeguage.com', path: '/' }; |