A value is Javascript will always be of some data type. It could be a string, a number, an object etc.
In this blog I will talk about the data types present in javascript, so by the end of this blog you should know all the data types in javascript.
There are 8 basic data types in JavaScript.
-
numberfor numbers of any kind: integer or floating-point, integers are limited by±(2531). -
bigintis for integer numbers of arbitrary length. -
stringfor strings. A string may have zero or more characters, there’s no separate single-character type. -
booleanfortrue/false. -
nullfor unknown values – a standalone type that has a single valuenull. -
undefinedfor unassigned values – a standalone type that has a single valueundefined. -
objectfor more complex data structures. -
symbolfor unique identifiers.
The typeof operator allows us to see which type is stored in a variable.
- Usually used as
typeof x, buttypeof(x)is also possible. - Returns a string with the name of the type, like
"string". - For
nullreturns"object"– this is an error in the language, it’s not actually an object.
Lets look at each of them and understand what they are...
Number
The number type represents both integer and floating point numbers (A floating point number, is a positive or negative whole number with a decimal point)
We can perform many operations on numbers like addition + , subtraction - , multiplication * and more
Besides regular numbers, the number data type also has so-called “special numeric values” which also belong to this data type:
-
Infinity, represents the mathematical Infinity ∞. It is a special value that’s greater than any number. (We can get it as a result of division by zero, Or just reference it directly) -Infinity-
NaN. represents a computational error. It is a result of an incorrect or an undefined mathematical operation, (Any further mathematical operation onNaNreturnsNaN)
alert( 1 / 0 ); // Infinity
alert( Infinity ); // Infinity
alert( NaN + 5 ); // NaN
alert( 8 * NaN ); // NaN
alert( "not a number" / 6 - 4 ); // NaN
Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
The script will never stop with a fatal error. At worst, we’ll get NaN as the result.
BigInt
In JavaScript, the “number” type cannot represent integer values larger than (253-1) (that’s 9007199254740991), or less than -(253-1) for negatives. It’s a technical limitation caused by their internal representation.
For most purposes that’s enough, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.
To solve this the BigInt type was recently added to the language to represent integers of arbitrary length.
A BigInt value is created by appending n to the end of an integer:
// the "n" at the end means it's a BigInt
const bigInt = 12455345678901544454524543456766644454545678901234567890n;
String
A string may consist of zero characters (be empty), one character or many of them. A string in JavaScript must be surrounded by quotes.
In JavaScript, there are 3 types of quotes.
- Double quotes:
"Hello". - Single quotes:
'Hello'. - Backticks:
Hello.
Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.
Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…},
let name = "Ved";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, Ved!
// embed an expression
alert( `the result is ${5 + 2}` ); // the result is 7
The expression inside ${…} is evaluated and the result becomes a part of the string. We can put anything in there: a variable like name or an arithmetical expression like 6 + 2 or something more complex.
Please note that this can only be done in **backticks**.
Boolean
The boolean type has only two values: true and false. It is also known as the logical type as this is commonly used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.
let listItemOneChecked = true; // yes, listItemOne is checked
let listItemTwoChecked = false; // no, listItemTwo is not checked
let isGreaterOne = 7 > 1;
let isGreaterTwo = 7 > 8;
alert( isGreaterOne ); // true (the comparison result is "yes")
alert( isGreaterTwo ); // false (the comparison result is "yes")
Null
In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages. It forms a separate type of its own which contains only the null value:
It’s just a special value which represents “nothing”, “empty” or “value unknown”.
let age = null;
The code above states that age is unknown.
undefined
Javascript also have a special value undefined . It makes a type of its own, just like null. The meaning of undefined is “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined:
let age = 100;
// change the value to undefined
age = undefined;
alert(age); // "undefined"
Technically, it is possible to explicitly assign undefined to a variable:
Objects and Symbols
-
The object type is special as all other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). But, objects are used to store collections of data and more complex entities.
let user = new Object(); // "object constructor" syntax
let user = {}; // "object literal" syntax
Symbol
The symbol type is used to create unique identifiers for objects. A “symbol” represents a unique identifier. A value of this type can be created using Symbol():
let id1 = Symbol("id");
let id2 = Symbol("id");
alert(id1 == id2); // false
typeof
The typeof operator returns the type of the argument.
A call to typeof x returns a string with the type name:
typeof undefined // "undefined"
typeof 0 // "number"
typeof 10n // "bigint"
typeof true // "boolean"
typeof "foo" // "string"
typeof Symbol("id") // "symbol"
typeof Math // "object" (1)
typeof null // "object" (2)
typeof alert // "function" (3)
The last three lines may need additional explanation:
-
Mathis a built-in object that provides mathematical operations. - The result of
typeof nullis"object". The behavior oftypeofis wrong here. That’s an officially recognized error intypeof, coming from very early days of JavaScript and kept for compatibility. Definitely,nullis not an object. It is a special value with a separate type of its own. - The result of
typeof alertis"function", becausealertis a function. That also comes from the early days of JavaScript. Technically, such behavior isn’t correct, but can be convenient in practice. There’s no special “function” type in JavaScript. Functions belong to the object type. Buttypeoftreats them differently, returning"function".
To put it clear: typeof is an operator, not a function.
If you’ve managed to read till here, do drop a like 😆
Top comments (0)