Câu hỏi phỏng vấn Javascript
Câu hỏi

Explain the difference between undefined and not defined in JavaScript

Câu trả lời

In JavaScript, the terms "undefined" and "not defined" refer to two distinct states that can occur when working with variables.

Undefined

The term "undefined" in JavaScript is used to describe a variable that has been declared but has not been assigned a value. When a variable is declared in JavaScript, the JavaScript engine automatically initializes it with the value of undefined. This is a special value in JavaScript that signifies that the variable lacks a value. The undefined value is actually a primitive value and is one of JavaScript's primitive types.

For example, if you declare a variable var a; and then log it to the console without assigning any value to it, console.log(a); will output undefined because the variable a has been declared but not initialized with a value[1][2][5].

Not Defined

On the other hand, "not defined" refers to a ReferenceError that occurs when you attempt to access a variable that has not been declared at all within the relevant scope. This means that the variable does not exist in the memory space and the JavaScript engine cannot find any reference to it.

For instance, if you try to log a variable that has not been declared, console.log(b);, JavaScript will throw a ReferenceError stating that b is not defined because there is no variable b in the current execution context[1][2][5].

Key Differences

  • Initialization: "undefined" is a value that is automatically assigned to variables that are declared but not initialized. "not defined" is an error that occurs when a variable has not been declared at all.
  • Type: `unde...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Explain Function.prototype.bind .

junior

Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?

middle

Could you explain the difference between ES5 and ES6

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào