js hoisting
/*Hoisting is JavaScript's default behavior of moving
all declarations to the top of the current scope (script or function).
Be carefull that only declaration gets hoisted NOT the initialitations*/
var x = 5;
alert("x is = "+x+". y is = "+y);//result => x is = 5. y is = undefined.
var y = 7;
/*
note that the code doesn't produce the error "y is not defined" like
it would if we would omit y. It executes but not in the way you would want.
*/
// Example 1
// Only y is hoisted
x = 1; // Initialize x, and if not already declared, declare it - but no hoisting as there is no var in the statement.
console.log(x + " " + y); // '1 undefined'
// This prints value of y as undefined as JavaScript only hoists declarations
var y = 2; // Declare and Initialize y
// Example 2
// No hoisting, but since initialization also causes declaration (if not already declared), variables are available.
a = 'Cran'; // Initialize a
b = 'berry'; // Initialize b
console.log(a + "" + b); // 'Cranberry'
Also in JavaScript:
- tilt js vue
- js unique using set
- Expected an assignment or function call and instead saw an expression
- javascript detect mobile device
- cypress fixture
- Truthy and Falsy js
- javascript check if object is null or empty
- how to check if an element exists in an array of objects js
- change favicon with javascript
- website login and registartion source code jsp java
- convert excel file to json using node js
- javascript move array element to front
- jquery addeventlistener wheel
- jquery change font color
- javascript scroll to bottom of div
- javascript eval passing variable
- JavaScript banner
- how to find out which version of React
- jquery get version in console
- difference between var let and const in javascript with example
- date difference
- js make radio checked
- how to make a deck of cards in javascript
- how to test useeffect with enzyme