Sitemap

Data Types in JavaScript

5 min readOct 7, 2021

--

Press enter or click to view image in full size

In JavaScript, data types explain the various forms of data that you will be working with and storing in variables. In JavaScript, a value is always of a specific type of data, and there are eight different types of data. These are as follows:

=> Number

=> BigInt

=> String

=> Boolean (logical type)

=> The “null” value

=> The “undefined” value

=> Objects and Symbols

=> The typeof operator

1. Number

The number data type is used to represent positive or negative integers with or without a decimal place, as well as values expressed in exponential notation, for example, 3.5e-4 (equivalent to 3.5x10–4).

For example:

Infinity, -Infinity, and NaN are all special values in the Number data type. Infinity symbolizes the mathematical Infinity, which is greater than any number and is obtained by dividing a non-zero number by zero, as seen below:

While NaN denotes a unique Not-a-Number value. It is caused by an incorrect or undefined mathematical operation, such as taking the square root of -1 or dividing 0 by 0, for example.

2. BigInt

In JavaScript, the “number” type cannot express integer values more than (253–1) (that is, 9007199254740991), or negative values less than -(253–1). It’s a technical constraint caused by the way they’re represented inside. That is sufficient for most uses, but there are instances when we want extremely large numbers, such as for cryptography or microsecond-precision timestamps.

To express integers of any length, the BigInt data type was recently added to the language. It gets its value by adding n to the end of an integer.

3. String Data Type

Get Fatomi Abiodun’s stories in your inbox

Join Medium for free to get updates from this writer.

Textual data is represented by the string data type (i.e., sequences of characters). Strings are made by enclosing one or more characters in single or double quotes, as shown below:

Note that the quotes can be used inside the string as long as they don’t match the quotes around them.

4. Boolean (logical type)

There are just two possible values for the Boolean data type: true or false. As seen below, it is commonly used to hold values such as yes (true) or no (false), on (true) or off (false), and so on.

Press enter or click to view image in full size

Comparisons in a program can also result in Boolean values. In the following example, two variables are compared and the result is displayed in an alert dialog box:

5. The “null” value

This is another unique data type that can only have one value: null. A null value denotes the absence of any value. It’s not the same as an empty string (“”) or zero; it’s just nothing. By explicitly giving the null value to a variable, it can be deliberately emptied of its present contents.

6. The “undefined” value

The special value undefined is the only value that may be assigned to the undefined data type. When a variable is declared but not assigned a value, the variable has the value undefined.

Press enter or click to view image in full size

7. Objects Data Type

The object is a sophisticated data type that lets you store data in collections. The attributes of an object are specified as a key-value pair. The key (name) of a property is always a string, but the value can be any data type, including strings, integers, booleans, and sophisticated data types like as arrays, functions, and other objects.

The following example shows the most basic method of creating an object in JavaScript.

If the property name is a valid JavaScript name, you can remove the quotations surrounding it. That is, quotations are necessary around “first-name,” but not around the first name. As an example, the vehicle object in the previous example may be expressed as:

8. The Typeof Operator

The typeof operator can be used to determine the type of data contained in a variable or operand. (typeof(x) or typeof x) can be used with or without parentheses.

The typeof operator is particularly useful when you need to handle values of different kinds in different ways, but it must be used with caution since it might give unexpected results in some circumstances, as seen in the following example:

The result of typeof null is “object,” as we can see. This is a typeof behavior fault that dates back to the early days of JavaScript and has been preserved for compatibility. Null is unquestionably not an object. It is a one-of-a-kind value with its own type.

--

--