Primitives and Objects
In JavaScript, variables can store two types of values:- primitives and objects. Understanding the difference between these two types is important for writing effective and efficient code.
Primitives
In JavaScript, primitive values are the most basic data types that can be stored in a variable. There are six primitive types in JavaScript:
String: Represents a sequence of characters, (e.g: "hello").Number: Represents a numeric value, (e.g: e.g: 42).Boolean: Represents a logical value, either true or false.Undefined: Represents an undefined value.Null: Represents a null value.Symbol: Represents a unique value.
Here are some examples of primitive values:
let str = "hello"; // string
let num = 42; // number
let bool = true; // boolean
let undef = undefined; // undefined
let nul = null; // null
let sym = Symbol("foo"); // symbolPrimitives are immutable, which means that once they are created, they cannot be changed. For example:
let x = 10;
x = 20; // x is now 20In this example, the value of x is changed from 10 to 20. However, this does not change the value of the primitive itself, but rather creates a new primitive with the value of 20.
Objects
Objects are more complex data types that can store collections of key-value pairs. In JavaScript, objects are used to represent more complex data structures and are created using curly braces {}. For example:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
};Objects can contain properties that are either primitive values or other objects. For example:
let person = {
name: "John",
address: {
street: "123 Main St",
city: "Anytown",
},
};Objects are mutable, which means that their properties can be changed after they are created. For example:
let person = {
name: "John",
age: 30,
};
person.age = 40; // person's age is now 40In this example, the value of the age property of the person object is changed from 30 to 40.
There are several other data types in JavaScript that are classified as objects, including arrays, functions, and dates. These data types behave similarly to objects in that they are mutable and can be modified after they are created.
Conclusion
In summary, primitives are the simplest data types in JavaScript and are immutable. Objects are more complex data types that are used to represent real-world objects or abstract concepts and are mutable. It is important to understand the differences between these two types of data in order to write effective and maintainable code in JavaScript.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
Variable Naming Rules
In JavaScript, There are a few rules that you need to follow when naming variables. These rules are important to ensure that your code is readable and maintainable.
Operators and Expressions
In JavaScript, operators are used to perform operations on variables and values. Expressions are combinations of variables, values, and operators that are evaluated to produce a result.