Home > Web Front-end > JS Tutorial > body text

Undefined Vs Not defined

Linda Hamilton
Release: 2024-11-23 21:35:15
Original
292 people have browsed it

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
Copy after login

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
Copy after login

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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template