Sobes.tech
Junior

Mis on määramata (undefined) muutujad JavaScriptis?

sobes.tech AI

Vastus AI-lt

In JavaScript, a variable is considered "undefined" if it has not been assigned a value. This is a special value of type undefined that indicates the absence of a value.

For example:

let a;
console.log(a); // undefined

The undefined value can appear in several cases:

  • The variable is declared but not initialized.
  • The function does not explicitly return a value — it returns undefined.
  • Accessing a non-existent property of an object.

It is important not to confuse undefined with null — null indicates an intentional absence of a value, while undefined indicates the absence of a default value.

Also, remember that during hoisting of variable declarations with var, the variable exists but is initialized with undefined until a value is assigned to it.