Table of Contents
JavaScript is a dynamic programming language that supports various data types. In this tutorial, we will discuss the different data types in JavaScript.
Data Types in JavaScript
JavaScript supports the following data types:
- Primitive Data Types: Primitive data types are the basic data types in JavaScript that are not objects.
- Non-Primitive Data Types: Non-primitive data types are objects in JavaScript.
Primitive Data Types
- Number: This data type represents numeric values. JavaScript treats all numeric values as floating-point numbers.
Example:
let x = 10;
let y = 3.14;
- String: This data type represents a sequence of characters enclosed in single or double quotes.
Example:
let firstName = 'John';
let lastName = "Doe";
- Boolean: This data type represents a logical value that can be either
true
orfalse
.
Example:
let isStudent = true;
let isGraduated = false;
- Null: This data type represents a null or empty value.
Example:
let myValue = null;
- Undefined: This data type represents a variable that has been declared but not assigned a value.
Example:
let myVariable;
- Symbol: This data type represents a unique value that is not equal to any other value.
Example:
let mySymbol = Symbol('mySymbol');
Non-Primitive Data Types
- Object: This data type represents a collection of related data or functionality.
Example:
let person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
- Array: This data type represents an ordered collection of values.
Example:
let myArray = [1, 2, 3, 4, 5];
- Function: This data type represents a reusable block of code that performs a specific task.
Example:
function addNumbers(x, y) {
return x + y;
}
Typeof Operator
JavaScript provides the typeof
operator to determine the data type of a variable.
Example:
let x = 10;
let y = 'Hello';
console.log(typeof x); // Output: number
console.log(typeof y); // Output: string
Conclusion
In this tutorial, we discussed the different data types in JavaScript. Understanding data types is essential when working with JavaScript because it helps you to write efficient and effective code.