JavaScript Data Types
JavaScript has several data types which define the type of value that a variable can store. we’ll go over each of these data types in detail to help you better understand how to work with them in your code.
JavaScript Primitive data Type
The main primitive data types in JavaScript are:
1. String: for storing text data.
2. Number: for storing numeric values, including integers and floating-point numbers.
3. Boolean: for storing values that can be either true
or false
.
4. Undefined: for representing a value that has not been assigned.
5. Null: for representing an intentional non-value.
6. Symbol: for creating unique and immutable object properties (available in ES6).
- String A string is a sequence of characters and is used to store text data. Strings are declared using quotes, either single quotes ('') or double quotes (""). For example:
1let name = "William Max"; 2let message = 'Hello World!';
- Number Numbers are used to store numeric values in JavaScript. Unlike some other programming languages, JavaScript has a single type for all numbers, both integers and floating-point. For example:
1let age = 30; 2let price = 19.99;
- Boolean A boolean data type can have one of two values: true or false. Booleans are often used in conditional statements to check the truthiness of an expression. For example:
1let isActive = true; 2let isAdmin = false;
- Undefined The undefined data type represents a value that has not been assigned to a variable. If a variable is declared but not assigned a value, it will be undefined. For example:
1let x; 2console.log(x); // Output: undefined
- Null The null data type represents an intentional non-value. This is often used to explicitly set a variable to have no value. For example:
1let y = null; 2console.log(y); // Output: null
- Symbol (ES6) A symbol is a unique and immutable primitive data type in JavaScript. It is used to create object properties that are unique and not enumerable. For example:
1let id = Symbol('id'); 2let obj = { 3 [id]: 123 4};
JavaScript Non-Primitive Data Types
Non-primitive data types are used to store complex data structures that are not easily represented by primitive data types.
- Object An object in JavaScript is an unordered collection of key-value pairs. Objects are declared using curly braces
{}
and can store a variety of data types, including primitive and non-primitive data types. For example:
1let user = { 2 name: "William Max", 3 age: 22, 4 isAdmin: false 5};
In the example above, user
is an object with three properties: name
, age
, and isAdmin
. Properties in an object are accessed using dot notation or square bracket notation. For example:
1console.log(user.name); // Output: "William Max" 2console.log(user["age"]); // Output: 22
- Array An array in JavaScript is an ordered collection of values. Arrays are declared using square brackets
[]
and can store a variety of data types, including primitive and non-primitive data types. For example:
1let numbers = [1, 2, 3, 4, 5]; 2let names = ["William", "Max", "Kim"];
In the example above, numbers
is an array of numbers and names
is an array of strings. Elements in an array can be accessed using index notation. For example:
1console.log(numbers[0]); // Output: 1 2console.log(names[2]); // Output: "Kim"