Explain Null and Undefined in JavaScript
Explain Null and Undefined in JavaScript
In JavaScript, null and undefined are both primitive values that represent the absence of a specific value, but they are used in different contexts and have different implications in the language.
undefined is a primitive value that is automatically assigned to variables that have been declared but have not yet been assigned a value. If you define a variable without initializing it, it will have the value undefined. It is also the default return value for functions that do not explicitly return anything, and the value you get when you try to access an object property or array element that does not exist.
Here's an example of undefined:
let uninitializedVariable;
console.log(uninitializedVariable); // Output: undefined
The typeof operator returns "undefined" when applied to an undefined value:
console.log(typeof uninitializedVariable); // Output: "undefined"
null, on the other hand, is an intentional absence of any object value. It is often used to indicate that a variable should hold an object but currently does not. Unlike undefined, null must be assigned explicitly. It is commonly used to denote that a variable is empty or to represent the end of a chain of object references.
Can you describe the main difference between a .forEach loop and a .map() loop and why you would pick one versus the other?
Xem chi tiếtCan you give an example for destructuring an object or an array in ES6?
Xem chi tiếtCan you give an example of a curry function and why this syntax offers an advantage?
Xem chi tiếtĐăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.