DEV Community

Discussion on: Some Strange Concept of JavaScript

Collapse
 
j471n profile image
Jatin Sharma

Well, I don't think that is the case. we can use undefined as a variable but we should not. And yes we can assign the value to it until and unless it is const. Here is the demo -

demo

This image shows that we can use undefined as a variable name. I have not declared it because browser's console declare it by default to undefined .

by default

If you wish to use it *.js file you can also do that

let undefined = 3;       // `var` also works fine but not `const`
console.log(undefined);         // output : 3
undefined = 9;
console.log(undefined);         // output : 9
Enter fullscreen mode Exit fullscreen mode

The above example shows that you can use undefined as long as it is not const because const is immutable. Since we are using it as a varible name.