JavaScript Data Types


Data types define what forms of data may be stored and handled within a program.

In JavaScript, there are six fundamental data types that may be grouped into three categories: primitive (or primary), composite (or reference), and special data types. Primitive data types include String, Number, and Boolean. Object, Array, and Function (all object types) are composite data types. Undefined and Null, on the other hand, are special data types.

Example

Preview

Primitive data types can only store a single value at a time, whereas composite data types can store collections of values as well as more complex entities. Let's go over each one in detail.


1. String

A string is a character sequence. Strings in JavaScript can be encased in single or double quotes. 

Example

Preview

2. Integers

The number data type is used to represent positive or negative values with or without a decimal point, as well as numbers expressed in exponential notation, for example, 1.5e-4 (equivalent to 1.5x10-4). Infinity, -Infinity, and NaN are all special values in the Number data type. Infinity is the mathematical infinity that is greater than any number.

Example

Preview

3. Boolean

The Boolean data type has only two possible values: true or false. It is commonly used to hold values such as yes (true) or no (false), on (true) or off (false), and so on. Boolean values are also the outcome of comparisons in a Program.

Example

Preview

4. Array

An array is a type of object that allows you to store several values in a single variable. Each value (also known as an element) in an array has a numeric location known as its index and can include any data type-numbers, texts, booleans, functions, objects, and even other arrays. The array index begins at zero, so the first array element is arr[0], not arr[1].

Example

Preview

5. Object

An object is a sophisticated data type that allows you to save data collections. An object has properties, which are specified as a key-value pair. A property key (name) is always a string, but the value can be any data type, including strings, integers, and booleans, as well as sophisticated data types such as arrays, functions, and other objects. Objects will be covered in greater detail in subsequent chapters.

Example

Preview

6. Undefined

The undefined data type can only have one value the undefined special value. If a variable is declared but not assigned a value, its value is undefined.

Example

Preview

7. Function

A function is a callable object that performs code. Because functions are objects, they may be assigned to variables.

Example

Preview

8. Empty Value

Another data type that can only have one value the null value. A null value indicates that no value exists. It's not the same as an empty string (" ") or 0 it's just nothing.

Example

Preview

FAQs

JavaScript has several built-in data types, including primitive types and reference types. The primitive types in JavaScript are number, string, boolean, null, undefined, and symbol (introduced in ES6). Reference types include objects and arrays. Understanding these data types is essential as they determine how data is stored and manipulated in JavaScript, allowing developers to work with different kinds of values and perform operations accordingly.

JavaScript provides the typeof operator to determine the data type of a variable or value. Using typeof followed by the variable or value in parentheses, you can obtain a string representation of the data type. For example, typeof 42 would return "number," typeof "Hello" would return "string," and typeof true would return "boolean." It's important to note that typeof null returns "object," which is a historical quirk in JavaScript.

Yes, JavaScript is a dynamically typed language, which means that variables can hold values of different data types during runtime. Unlike statically typed languages where variables are bound to a specific data type, JavaScript allows you to assign different data types to variables as needed. For example, you can initially assign a number to a variable and later assign a string to the same variable without encountering any type-related issues. This flexibility can be powerful but also requires careful attention to avoid unexpected behavior when working with variables that change data types.