JavaScript Data Types

JavaScript is a dynamically typed language, which means that you do not need to declare the data type of a variable before assigning it a value. The JavaScript engine will automatically determine the data type based on the value that is assigned.

There are seven primitive data types in JavaScript:

Number: Any numeric value, such as 10, 3.14, or -5.67

String: A sequence of characters, such as “Hello, world!” or ‘This is a string.’

Boolean: A logical value, either true or false

Null: A special value that represents the absence of a value

Undefined: A special value that represents a variable that has not yet been assigned a value

BigInt: A new data type (ES2020) that can be used to store integer values that are too big to be represented by a normal JavaScript Number

Symbol: A unique identifier that can be used to add unique property keys to an object

There is also one non-primitive data type:

Object: A collection of properties and values

Primitive Data Types

Primitive data types are stored in the call stack, which is a region of memory that is used to store function calls. Primitive data types are immutable, which means that they cannot be changed once they are created.

Number

The Number data type can represent both integers and floating-point numbers. JavaScript numbers are stored in a 64-bit floating-point format, which means that they can represent a very wide range of values.

Examples

const number1 = 10;
const number2 = 3.14;
const number3 = -5.67;

String

The String data type represents a sequence of characters. Strings can be created using single quotes (‘) or double quotes (“).

Examples

const string1 = "Hello, world!";
const string2 = 'This is a string.';

Boolean

The Boolean data type represents a logical value, either true or false.

Examples

const boolean1 = true;
const boolean2 = false;

Null

The Null data type represents the absence of a value.

Examples

const nullValue = null;

Undefined

The Undefined data type represents a variable that has not yet been assigned a value.

Examples

let undefinedVariable;

BigInt

The BigInt data type (ES2020) can be used to store integer values that are too big to be represented by a normal JavaScript Number.

Examples

const bigInt1 = 900719925124740999n;
const bigInt2 = 1n;

Symbol

The Symbol data type (ES2015) can be used to create unique identifiers that can be used as property keys on objects.

Examples

const symbol1 = Symbol('hello');
const symbol2 = Symbol('hello');

Object Data Type

The Object data type is the only non-primitive data type in JavaScript. Objects are used to store collections of properties and values.

Examples

const object1 = {
name: "John Doe",
age: 30,
occupation: "Software Engineer"
};

const object2 = new Object();

Objects can also be used to represent more complex data structures, such as arrays, lists, and trees.

Data Type Conversion

JavaScript provides a number of functions for converting data types. For example, the parseInt() function can be used to convert a string to a number, and the toString() function can be used to convert a number to a string.

Examples

const numberString = "10";
const number = parseInt(numberString);

const age = 30;
const ageString = age.toString();

Conclusion

JavaScript data types are an important part of the language. By understanding the different data types and how to use them, you can write more efficient and effective JavaScript code.