It’s good for beginners, but sometimes it creates a lot of confusion.
It works fine even if you don’t declare the variable and directly initialize it.
I have worked with Java before, and when I compare the two languages, I get totally confused about why does JavaScript run with this unstrict code
Both of these scenarios function properly. HOW COME?
a = 5
b = 4
c = a + b
console.log(c)
and
let a = 5
let b = 4
let c = a + b
console.log(c)
2
Answers
JavaScript, by nature, is a very loose and flexible language. For some people, TypeScript is a better alternative, because it is much more strict.
Variables
Without Variable Declaration (
a = 5
,b = 4
,c = a + b
):In JavaScript, if you assign a value to a variable without declaring it using
var
,let
, orconst
, the variable is automatically declared in the global scope. So, in your first scenario:JavaScript will interpret it as if you had declared
a
,b
, andc
as global variables. (This is not recommended because it can lead to naming conflicts)With Variable Declaration (
let a = 5
,let b = 4
,let c = a + b
):In this scenario, you explicitly declare each variable’s scope using the
let
keyword. This is the recommended way to declare variables in modern JavaScript. It keeps variables confined to the scope in which they are announced, preventing global namespace pollution (and other issues).let
orconst
within curly braces{}
are only accessible within that block.Semicolons
JavaScript does not strictly require semicolons because it has Automatic Semicolon Insertion (ASI) as part of its grammar. JavaScript is whitespace-sensitive, so line breaks are often enough to signify the end of a statement.
Just to add to @RedYetiDev’s excelent answer, Javascript does have a "strict mode". You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
By default, Javascript operates in non-strict mode, or as MDN puts it "slopy mode" in it, undeclared variables are assumed as global variables (or in the browser context, created inside the
window
object.In strict mode, this (among other things) are changed. I quote:
so even though the code in your example would work, if you put it in strict mode it would fail
so strict mode behaves a little bit more as you expect if you come from more stricter languages like Java, and its encouraged to use it for many reasons, including avoiding such mistakes and even improving performance (as the optimizer can sometimes add better optimizations with it)
Now why does javascript have a "sloppy mode" and a "strict mode"? because when strict mode was introduced js had been years in the wild so a change this big woul’ve broken many existing pages. So it was introduced under that "use strict" flag, that is fully retrocompatible (because older browsers that see this command would just ignore it, and by default strict code is compatible with slopy code -although not the other way around-) so that older sites would keep behaving the way they did before.
You will see this many times with JS, because as much as they want to improve the language, the compatibility must be maintain, this is the reason for the existance of things like
var a = 1;
andlet a = 1
.var
is the older version oflet
and even though they serve the same purpose, they behave differently (let behaving more like you expect if you come from Java) butvar
is kept for retrocompatibility as well.