(name is undefined)
You: What is name
? (*)
JavaScript: name
? What's a name
? I don't know what you're talking about. You haven't ever mentioned any name
before. Are you seeing some other scripting language on the (client-)side?
name = null;
You: What is name
?
JavaScript: I don't know.
In short; undefined
is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null
is where the thing is known to exist, but it's not known what the value is.
One thing to remember is that null
is not, conceptually, the same as false
or ""
or such, even if they equate after type casting, i.e.
name = false;
You: What is name
?
JavaScript: Boolean false.
name = '';
You: What is name
?
JavaScript: Empty string
*: name
in this context is meant as a variable which has never been defined. It could be any undefined variable, however, name is a property of just about any HTML form element. It goes way, way back and was instituted well before id. It is useful because ids must be unique but names do not have to be.
This article about Javascript Strict Mode might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More
To quote some interesting parts:
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
And:
Strict mode helps out in a couple ways:
- It catches some common coding bloopers, throwing exceptions.
- It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article):
// Non-strict code...
(function(){
"use strict";
// Define your library strictly...
})();
// Non-strict code...
Which might be helpful if you have to mix old and new code ;-)
So, I suppose it's a bit like the "use strict"
you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.
Strict mode is now supported by all major browsers.
Inside native ECMAScript modules (with import
and export
statements) and ES6 classes, strict mode is always enabled and cannot be disabled.
Best Answer
Scoping rules
The main difference is scoping rules. Variables declared by
var
keyword are scoped to the immediate function body (hence the function scope) whilelet
variables are scoped to the immediate enclosing block denoted by{ }
(hence the block scope).The reason why
let
keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.Take a look at this example from another stackoverflow question:
My value: 3
was output to console each timefuncs[j]();
was invoked since anonymous functions were bound to the same variable.People had to create immediately invoked functions to capture correct values from the loops but that was also hairy.
Hoisting
While variables declared with
var
keyword are hoisted (initialized withundefined
before the code is run) which means they are accessible in their enclosing scope even before they are declared:let
variables are not initialized until their definition is evaluated. Accessing them before the initialization results in aReferenceError
. The variable is said to be in "temporal dead zone" from the start of the block until the initialization is processed.Creating global object property
At the top level,
let
, unlikevar
, does not create a property on the global object:Redeclaration
In strict mode,
var
will let you re-declare the same variable in the same scope whilelet
raises a SyntaxError.