2.1 Variables are References
In JavaScript, variables are references to objects, not the variable itself. This means that when you create a variable in JavaScript, you're just creating a reference to an object in memory.
This feature has significant implications for working with variables, especially when passing objects to functions or assigning them to other variables.
When you create a variable and assign it a value, JavaScript allocates an object in memory for that value and makes the variable a reference to that object. If you assign one variable to another, the new variable will reference the same object as the original variable. This means that changes made through one variable will reflect on the other if the object is mutable.
Example:
let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a); // Outputs: [1, 2, 3, 4]
We added the number 4 to variable b, but it also got added to the list in variable a because both variables are just references to the array object [1, 2, 3].
2.2 The Undefined Type
In JavaScript, there are two special data types: null and undefined. These types often raise questions among developers due to their similarities, but they have different purposes and are used in different situations. Let's take a closer look at them.
Undefined Type
The undefined type represents the value of a variable that has been declared but not initialized. This value is also returned when accessing a non-existent property of an object or an array element that doesn't exist.
Examples
1. Uninitialized variable:
let x;
console.log(x); // undefined
2. Non-existent object property:
let obj = {};
console.log(obj.property); // undefined
3. Non-existent array element:
let array = [1, 2, 3];
console.log(array[5]); // undefined
4. Missing function argument:
function myFunction(a, b) {
console.log(b); // undefined, if called as myFunction(1)
}
myFunction(1);
Undefined Properties:
undefinedis a data type and a valuetypeof undefinedreturnsundefined
2.3 Null Type
The null type represents an intentional absence of value. This value is often used to initialize variables that will later be assigned an object or to clear a variable's value.
Examples
1. Variable initialization:
let obj = null;
console.log(obj); // null
2. Clearing a variable's value:
let user = { name: "Alice" };
user = null; // user no longer points to an object
Null Properties:
nullis a value representing the intentional absence of valuetypeof nullreturnsobject, which is a known error in JavaScript (null is not actually an object)
2.4 Comparing Null and Undefined
Although null and undefined can both be used to denote the absence of value, they differ in purpose and usage context.
| Property | null | undefined |
|---|---|---|
| Type | Primitive type | Primitive type |
| Purpose | Intentional absence of value | Uninitialized variable |
| Usage | Initializing variables, clearing values | Non-existent properties, array elements |
| Default value | No | Yes (for uninitialized variables, function parameters) |
Usage Examples
1. Initializing a variable with an object or array:
let myObject = null;
let myArray = null;
2. Checking values:
let value;
if (value === undefined) {
console.log("Variable is not initialized.");
}
let result = null;
if (result === null) {
console.log("No value present.");
}
3. Clearing values:
let person = { name: "Alice" };
person = null; // Now person no longer points to an object
4. Checking for the existence of a property:
let obj = {};
if (obj.property === undefined) {
console.log("Property does not exist.");
}
Understanding the differences between null and undefined in JavaScript is important for effective value management and avoiding errors in your code.
undefined typically indicates uninitialized variables or missing properties, whereas null is used to explicitly denote the absence of a value.
Using these special data types correctly makes your code more understandable and predictable.
GO TO FULL VERSION