Lesson 5 of 36 · 2 min
JavaScript Datatypes
Learn about JavaScript data types, including numbers, strings, booleans, arrays, objects, and more, with clear examples for each type
On this page
In JavaScript, data types define the kind of values that variables can hold and how those values behave. Understanding data types is fundamental to working with JavaScript effectively.
1. Number:
This data type is used for numeric values, which can be integers or floating-point numbers (decimals).
Example :
let age = 25;
let price = 9.99;
2. String:
Strings are used to represent text. You can enclose text in single (‘ ‘), double (” “), or backticks (`) quotes.
Example :
let greeting = "Hello, world!";
let name = 'Alice';
3. Boolean:
Boolean data types have only two possible values: true or false. They are used for logical operations and comparisons.
Example :
let isStudent = true;
let isLoggedIn = false;
4. Array:
Arrays are used to store collections of data, such as a list of numbers or strings. Elements in an array are accessed by their position (index) in the array.
Example :
let fruits = ["apple", "banana", "orange"];
5. Object:
Objects are used to store key-value pairs, allowing you to create more complex data structures.
Example :
let person = {
name: "John",
age: 30,
isStudent: false
};
6. Null:
The null data type represents the intentional absence of any object value. It’s often used to initialize variables that will hold an object later.
Example :
let emptyValue = null;
7. Undefined:
When a variable is declared but hasn’t been assigned a value, it has the undefined data type. It’s different from null in that it indicates a variable’s uninitialized state.
Example :
let jobTitle;
console.log(jobTitle); // Outputs "undefined"
8. Function:
Functions are a special data type in JavaScript, representing executable code blocks. You can declare functions and call them to perform specific tasks.
Exmaple :
function add(x, y) {
return x + y;
}
9. Symbol:
Symbols are unique and immutable values often used as object property keys to avoid naming conflicts.
Example :
const uniqueKey = Symbol('unique');
let user = {
name: "Alice",
[uniqueKey]: 12345
};
10. BigInt:
The BigInt data type is used for large integers that cannot be represented accurately with the standard Number data type. It’s denoted by appending n to a numeric literal like 10n.
Example :
const bigNumber = 1234567890123456789012345678901234567890n;
Understanding these data types helps us work with different kinds of information in our JavaScript programs. It’s essential to choose the appropriate data type for each variable or value to ensure our code behaves as intended. JavaScript’s dynamic typing allows variables to change data types during runtime, so it’s crucial to be aware of how data types behave in various contexts.
More from JavaScript Tutorial
- Lesson 7 · 5 minIf…else StatementLearn about JavaScript’s if, else, and else if statements: fundamental tools for making decisions in your code and creating dynamic applications.
- Lesson 8 · 3 minSwitch CaseExplore the power of JavaScript’s switch case statement in decision-making with practical examples and use cases in this comprehensive guide
- Lesson 9 · 2 minWhile LoopExplore the JavaScript while loop, a fundamental construct for automating tasks based on conditions. Learn its syntax and common use cases