Hey there! In the last post, we covered how comments are used and variables are declared in JavaScript. Today, we'll explore data types in JavaScript. Let's get started.
JavaScript provides two main types of data types:
- Primitive Data Types
- Non-Primitive Data Types
Note: JavaScript is a dynamically typed language, so you don't need to specify the data type of a variable. Simply declare it using var
, let
, or const
.
Primitive Data Types
Primitive data types are basic types that are not objects and do not have methods. They are immutable, meaning their values cannot be changed. When you manipulate a primitive value, you're working directly with its value rather than with a reference. Primitive values are stored directly in memory.
The six primitive data types are:
- String: Represents a sequence of characters.
let greeting = "hello";
- Number: Represents both integer and floating-point numbers.
let age = 42;
let pi = 3.14;
-
Boolean: Represents a logical entity, either
true
orfalse
.
let isAvailable = true;
- Null: Represents the intentional absence of any value.
let emptyValue = null;
- Undefined: Represents a variable that has been declared but not assigned a value.
let notAssigned;
- Symbol: Represents a unique identifier (introduced in ECMAScript 6).
let uniqueID = Symbol("id");
Non-Primitive Data Types
Non-primitive data types in JavaScript can store collections of values and have methods and properties. They are mutable, meaning their values can be changed.
The main non-primitive data types are:
- Object: Complex data types that can hold key-value pairs.
const person = {
name: "John",
age: 30,
hobbies: ["reading", "coding"],
address: {
city: "New York",
country: "USA"
},
sayHello: function() {
console.log("Hello!");
}
};
- Array: Ordered collections of values.
const fruits = ["apple", "banana", "orange"];
- Function: Special type of object that can be invoked to perform a task or calculate a value.
function add(a, b) {
return a + b;
}
- Date: Represents a specific date and time.
const currentDate = new Date();
Thatβs all for this post. In the next post, we will discuss operators and conditional statements. Stay connected and donβt forget to follow me!
Top comments (1)
You're missing
BigInt
from the primitive types.