JavaScript is a flexible programming language that can work with different data types. These data types can be stored in variables and processed. Here are the fundamental data types in JavaScript:
1. String
The string data type represents text values. A string can be defined using single quotes ('
), double quotes ("
), or backticks (`
).
let name = 'John';
let message = "Hello, World!";
let templateString = `My name is ${name}.`;
2. Number
The number data type represents numeric values such as integers, decimals, and negative numbers.
let integer = 42;
let decimal = 3.14;
let negativeNumber = -10;
3. Boolean
The boolean data type represents only two values: true
(true) and false
(false).
let isTrue = true;
let isFalse = false;
4. Array
The array data type represents a collection of elements. Elements are defined within square brackets and separated by commas
let numbers = [1, 2, 3, 4, 5];
let names = ['John', 'Jane', 'Alice'];
5. Object
The object data type represents a combination of properties and values. Properties are defined as key-value pairs.
let person = {
name: 'John',
age: 30,
city: 'New York'
};
6. Undefined and Null
The undefined
data type indicates that a variable has not been assigned a value, whereas the null
data type represents an empty value.
let undefinedVariable;
let nullValue = null;
JavaScript also provides more complex data types beyond these basics. However, these fundamental data types form the building blocks of most programs.
Conclusion
JavaScript is a flexible language with various data types, each with unique usage and properties. In this article, we examined the fundamental data types in JavaScript along with examples.