Hoisting
(meaning, recognizing a variable prior to its declaration):
Javascript Hoists only declaration(var x;).
But , not initiations(var x=9; (or) x=9);
Example:
(a)
<body>
<p id="demo"></p>
<script>
x = 5; // Assign/initialize 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
var x; // Declare x
</script>
</body>
(b)
<body>
<p id="demo"></p>
<script>
var x; // Declare x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
x = 5; // Assign/initialize 5 to x
</script>
</body>
(a) results in "5", which is the value assigned for "x", though x has been defined after it is used. However, x has been initialized much before its usage.
(b) results in "undefined", though "x" has been defined before its usage. However, x has been initialized after its usage.
Hence, javascript hoists only for initializations and not declarations.
___________________________________
use strict;
This statement is used in order to ensure that the developer initializes and declares all his variables before its usage.
The "use strict" directive is only recognized at the beginning of a script or a function.
refer: http://www.w3schools.com/js/js_strict.asp to check onmore of the keyword notes
_________________________________________________________
No comments:
Post a Comment