In JavaScript, you can assign various types of data to a variable, such as a string, number, boolean, and more. This is because JavaScript is a loosely typed (or dynamically typed) language — meaning variables are not bound to a specific data type.
Example: A Variable with Different Data Types
In this example:
-
1
is a number -
'one'
is a string -
true
is a boolean
This shows how JavaScript allows the same variable to hold different types of values at different times.
JavaScript Data Types
JavaScript data types are categorized into two main types:
-
Primitive Data Types
-
Structural (Non-Primitive) Data Types
Primitive Data Types
Primitive data types are the most basic kinds of data in JavaScript. They represent a single value and are immutable (cannot be changed).
Data Type | Description | Example |
---|---|---|
String | A sequence of characters enclosed in single ' ' , double " " , or backticks ` ` . | 'Hello' , "World" , `JavaScript` |
Number | Represents both integer and floating-point numbers. | 100 , 3.14 |
BigInt | Represents large integers beyond the safe limit of Number . It ends with n . | 123456789123456789123456789n |
Boolean | Represents a logical value: either true or false . | true , false |
Null | Represents an intentional absence of any object value. | let data = null; |
Undefined | A variable that has been declared but not assigned a value is undefined . | let x; console.log(x); // undefined |
Structural (Non-Primitive) Data Types
Structural data types are used to store collections of values or more complex entities.
Data Type | Description | Example |
---|---|---|
Object | A collection of key-value pairs (properties and methods). |
Note: Type Coercion in JavaScript
When adding a number and a string, JavaScript automatically converts the number to a string. This behavior is called type coercion.
Left-to-Right Evaluation in JavaScript
In JavaScript, expressions are evaluated from left to right. This means that the order in which you write the operations can impact the result. Understanding how JavaScript evaluates expressions is crucial for avoiding unexpected outcomes.
Explanation:
-
First,
5 + 10
is evaluated, which results in15
(a number). -
Next,
15 + "20"
is evaluated. Since one of the operands is a string, JavaScript converts the number15
to a string and concatenates it with"20"
, resulting in"1520"
.
Explanation:
-
First,
"5" + 10
is evaluated. JavaScript converts10
to a string and concatenates it with"5"
, resulting in"510"
. -
Then,
"510" + 15
is evaluated. JavaScript converts15
to a string and concatenates it with"510"
, resulting in"51015"
.
JavaScript Arrays
An Array is a special variable in JavaScript used to store a collection of values. It can hold multiple values of different types, including strings, numbers, objects, or even other arrays.
Key Characteristics of Arrays:
-
Arrays are ordered collections of data.
-
They can hold multiple values in a single variable.
-
They use indexing to access elements (indices start from
0
).
Creating Arrays
You can create arrays in JavaScript using either of the following methods:
1. Using Array Literals
2. Using the Array
Constructor
Accessing Elements in an Array
You can access elements in an array using their index. The index starts at 0
.
Example:
Array Properties and Methods
Arrays in JavaScript come with many built-in properties and methods that make it easy to manipulate data.
Common Array Properties
-
length
: Returns the number of elements in the array.
Common Array Methods
-
push()
: Adds one or more elements to the end of an array.
-
pop()
: Removes the last element from an array and returns it.
-
shift()
: Removes the first element from an array and returns it.
-
unshift()
: Adds one or more elements to the beginning of an array.
-
concat()
: Combines two or more arrays into a new array.
Iterating Over Arrays
JavaScript provides several ways to loop through arrays, such as using for loops or forEach.
1. Using a for loop
2. Using forEach()
Method
Multidimensional Arrays
You can also create arrays of arrays, which is known as a multidimensional array.
Example:
Array Methods for Searching
-
indexOf()
: Returns the index of the first occurrence of an element in the array.
-
includes()
: Returnstrue
if an array contains a specified element.
Array Destructuring
With array destructuring, you can easily extract values from arrays into variables.
Example:
Summary
-
Arrays in JavaScript are used to store multiple values in a single variable.
-
You can access elements using their index, and arrays have useful methods for modifying, searching, and iterating through data.
-
JavaScript arrays are dynamic, meaning they can grow or shrink in size as needed.
An Object in JavaScript is a collection of key-value pairs, where each key (also called a property) is a string, and its associated value can be any data type, including other objects, arrays, or functions. Objects allow you to store and organize data more flexibly than arrays.
Key Characteristics of Objects:
-
Objects are unordered collections of key-value pairs.
-
The keys (also called properties) are typically strings or symbols, and the values can be any data type (including arrays, numbers, strings, etc.).
-
Objects are useful for representing complex data structures, like a person, car, or book.
Creating Objects
There are two common ways to create objects in JavaScript: