Home >Web Front-end >JS Tutorial >Undefined Vs Not defined

Undefined Vs Not defined

Linda Hamilton
Linda HamiltonOriginal
2024-11-23 21:35:15369browse

Undefined Vs Not defined

First, we need to understand the JavaScript Code Execution process, which consists of two phases: the Memory Creation Phase and the Code Execution Phase. Both 'undefined' and 'not defined' are related to memory space.

undefined

In the Memory Creation Phase, variables and functions are stored as key-value pairs. JavaScript assigns undefined to each variable as a placeholder. This temporary value stays until JavaScript finds the real value, then it replaces undefined with the real one. Don't assign undefined to any variables; it's not a good practice.

var x ;
console.log(x);// undefined
x = 2;
console.log(x); //2

not defined

In JavaScript, if a variable is not declared at all and we try to access it, it will throw a ReferenceError.

console.log(x);//ReferenceError: x is not defined

credits to Akshay Saini https://youtu.be/B7iF6G3EyIk?si=0WQLx-yjVOgdkkIn

The above is the detailed content of Undefined Vs Not defined. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn