0
'use strict';
            
function NAME2 (){
  name = 'Smith';
  console.log(name);    
}
NAME2();

function NAME3 (){
  name = 'John'; 
  console.log(name);
}
NAME3();               

As you can see in both the functions, I havent used any variable keyword to define the variable thats somehow still executing.

What's wrong?

Or is it simply optional to use variable keyword in JS?

Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24

1 Answers1

2

Assigning to an undeclared variable is forbidden in strict mode, but in the context of a browser, name is predeclared in the global scope.

This would error if you were to run the code outside of a browser:

screenshot demonstrating error in Node.js

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335