Skip to content

Instantly share code, notes, and snippets.

@shreshthmohan
Created February 9, 2018 12:22
Show Gist options
  • Save shreshthmohan/f8e58b34ba319ea04ac54635a147b8d1 to your computer and use it in GitHub Desktop.
Save shreshthmohan/f8e58b34ba319ea04ac54635a147b8d1 to your computer and use it in GitHub Desktop.
Creation and hoisting - A little weird // source http://jsbin.com/vijiyer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Creation and hoisting - A little weird</title>
</head>
<body>
<script id="jsbin-javascript">
var a = "variable a";
function b() {
console.log('called function b');
};
b();
console.log(a);
console.log('Works as expected, yeah? \n Now let\'s try something that might not work');
c();
console.log(d);
var d = 7;
function c() {
console.log('called function c');
};
console.log('See what happened? We didn\'t get an error, we got d = undefined, but not 7 that we assigned to the variable d. And the function worked perfectly. \n Wonder why that happened? Before we explain let\'s try one more thing.');
try {
console.log(e);
} catch(err) {
console.log(JSON.stringify(err.name));
console.log(JSON.stringify(err.message));
}
console.log('Got an error! But we didn\'t get an error when logging d. Note: undefined is not the same as getting an error.');
console.log('Here\'s what is happening. Execution context is created in two phases. First, creation phase. this is created, global object, outer environment. In this creation phase, the parser recognizes variables and functions and allocates the memory spaces for those functions and variables. Before JS engine executes the code line-by-line, all variables and function have been created/provided memory space. functions entirely exist in memory right after the creation phase. But for the variables it\'s a little different, assignments happen in the execution phase. In the creation phase the JS engine assigns undefined to the variables. undefined is a special keyword, a special value and only means that the variable exists in memory and JS has given it a placeholder value.');
var f;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('undefined is special value/keyword, not a string, which signifies that the variable exists in memory, but hasn\'t been assigned by the programmer. It is not the same as not declaring a variable.');
f = 5;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('So, a programmer should never set a variable to undefined. It\'s valid javascript, but dangerous. We should let the meaning of undefined mean that that JS engine set it and the programmer didn\'t. Helps with debugging. Will let you know that you didn\'t set that variable. Use null instead for this purpose. So that the JS engine\'s placeholder is different from the programmer\'s.');
</script>
<script id="jsbin-source-javascript" type="text/javascript">var a = "variable a";
function b() {
console.log('called function b');
};
b();
console.log(a);
console.log('Works as expected, yeah? \n Now let\'s try something that might not work');
c();
console.log(d);
var d = 7;
function c() {
console.log('called function c');
};
console.log('See what happened? We didn\'t get an error, we got d = undefined, but not 7 that we assigned to the variable d. And the function worked perfectly. \n Wonder why that happened? Before we explain let\'s try one more thing.');
try {
console.log(e);
} catch(err) {
console.log(JSON.stringify(err.name));
console.log(JSON.stringify(err.message));
}
console.log('Got an error! But we didn\'t get an error when logging d. Note: undefined is not the same as getting an error.');
console.log('Here\'s what is happening. Execution context is created in two phases. First, creation phase. this is created, global object, outer environment. In this creation phase, the parser recognizes variables and functions and allocates the memory spaces for those functions and variables. Before JS engine executes the code line-by-line, all variables and function have been created/provided memory space. functions entirely exist in memory right after the creation phase. But for the variables it\'s a little different, assignments happen in the execution phase. In the creation phase the JS engine assigns undefined to the variables. undefined is a special keyword, a special value and only means that the variable exists in memory and JS has given it a placeholder value.');
var f;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('undefined is special value/keyword, not a string, which signifies that the variable exists in memory, but hasn\'t been assigned by the programmer. It is not the same as not declaring a variable.');
f = 5;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('So, a programmer should never set a variable to undefined. It\'s valid javascript, but dangerous. We should let the meaning of undefined mean that that JS engine set it and the programmer didn\'t. Helps with debugging. Will let you know that you didn\'t set that variable. Use null instead for this purpose. So that the JS engine\'s placeholder is different from the programmer\'s.');
</script></body>
</html>
var a = "variable a";
function b() {
console.log('called function b');
};
b();
console.log(a);
console.log('Works as expected, yeah? \n Now let\'s try something that might not work');
c();
console.log(d);
var d = 7;
function c() {
console.log('called function c');
};
console.log('See what happened? We didn\'t get an error, we got d = undefined, but not 7 that we assigned to the variable d. And the function worked perfectly. \n Wonder why that happened? Before we explain let\'s try one more thing.');
try {
console.log(e);
} catch(err) {
console.log(JSON.stringify(err.name));
console.log(JSON.stringify(err.message));
}
console.log('Got an error! But we didn\'t get an error when logging d. Note: undefined is not the same as getting an error.');
console.log('Here\'s what is happening. Execution context is created in two phases. First, creation phase. this is created, global object, outer environment. In this creation phase, the parser recognizes variables and functions and allocates the memory spaces for those functions and variables. Before JS engine executes the code line-by-line, all variables and function have been created/provided memory space. functions entirely exist in memory right after the creation phase. But for the variables it\'s a little different, assignments happen in the execution phase. In the creation phase the JS engine assigns undefined to the variables. undefined is a special keyword, a special value and only means that the variable exists in memory and JS has given it a placeholder value.');
var f;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('undefined is special value/keyword, not a string, which signifies that the variable exists in memory, but hasn\'t been assigned by the programmer. It is not the same as not declaring a variable.');
f = 5;
if (f === undefined) {
console.log('f is undefined');
} else {
console.log('f is defined and equal to:', f);
}
console.log('So, a programmer should never set a variable to undefined. It\'s valid javascript, but dangerous. We should let the meaning of undefined mean that that JS engine set it and the programmer didn\'t. Helps with debugging. Will let you know that you didn\'t set that variable. Use null instead for this purpose. So that the JS engine\'s placeholder is different from the programmer\'s.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment