1. Home
  2. >
  3. Declaring a variable without keyword in javascript?
2

Declaring a variable without keyword in javascript?
rahoolbenji

What if you define a variable without using a keyword at all? Technically, if x hasn’t already been defined, then x = 1 is shorthand for window.x = 1

To prevent this shorthand altogether, you can use strict mode — introduced in ES5 — by writing use strict at the top of your document or a particular function. Then, when you try to declare a variable without a keyword, you’ll get an error: Uncaught SyntaxError: Unexpected indentifier .

'use strict';

x = 1;
console.log(x);


# javascript