Skip to content

Instantly share code, notes, and snippets.

@cstroliadavis
Last active October 3, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cstroliadavis/52847f2f7bd6817d75b101972ffb6488 to your computer and use it in GitHub Desktop.
Save cstroliadavis/52847f2f7bd6817d75b101972ffb6488 to your computer and use it in GitHub Desktop.
JavaScript scripts are evaluated and run immediately. https://jsbin.com/nurica
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// # Things I wish I knew about JavaScript when I started:
// # The basics:
// ## Scripts are evaluated first and then run immediately.
// contents of functions are not evaluated until the function is called
// This will run immediately after the whole script has been evaluated
console.log('Is thisWillRunRightAwayLuckilyItWasEvaluatedFirst set already? ' + (undefined !== thisWillRunRightAwayLuckilyItWasEvaluatedFirst));
// Since the whole script was evaluated first, this already exists and can be run now
thisWillRunRightAwayLuckilyItWasEvaluatedFirst();
// This will run next
console.log('Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt set already? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// Okay, it's not defined yet. That's probably because evaluation doesn't process assignments yet
// This is the traditional way of declaring a function. It is evaluated once the script loads
function thisWillRunRightAwayLuckilyItWasEvaluatedFirst(){
console.log('This function is evaluated when the script loads. It can be run even before this declaration is reached.');
}
// This is another way of declaring a function, however, the only thing evaluated when
// the script loads is that we declared the variable. It did not actually assign the function
// to the variable yet
var thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt = function(){
console.log('We can\'t run this until after this point, since it has not actually been assigned yet.');
};
// This will run
console.log('How about now? Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt defined yet? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// It is clear that the function is now defined and can be run
thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt();
// After JS evaluates the script, it will do something referred to as "hoisting"
// More on this later
</script>
<script id="jsbin-source-javascript" type="text/javascript">// # Things I wish I knew about JavaScript when I started:
// # The basics:
// ## Scripts are evaluated first and then run immediately.
// contents of functions are not evaluated until the function is called
// This will run immediately after the whole script has been evaluated
console.log('Is thisWillRunRightAwayLuckilyItWasEvaluatedFirst set already? ' + (undefined !== thisWillRunRightAwayLuckilyItWasEvaluatedFirst));
// Since the whole script was evaluated first, this already exists and can be run now
thisWillRunRightAwayLuckilyItWasEvaluatedFirst();
// This will run next
console.log('Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt set already? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// Okay, it's not defined yet. That's probably because evaluation doesn't process assignments yet
// This is the traditional way of declaring a function. It is evaluated once the script loads
function thisWillRunRightAwayLuckilyItWasEvaluatedFirst(){
console.log('This function is evaluated when the script loads. It can be run even before this declaration is reached.');
}
// This is another way of declaring a function, however, the only thing evaluated when
// the script loads is that we declared the variable. It did not actually assign the function
// to the variable yet
var thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt = function(){
console.log('We can\'t run this until after this point, since it has not actually been assigned yet.');
};
// This will run
console.log('How about now? Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt defined yet? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// It is clear that the function is now defined and can be run
thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt();
// After JS evaluates the script, it will do something referred to as "hoisting"
// More on this later</script></body>
</html>
// # Things I wish I knew about JavaScript when I started:
// # The basics:
// ## Scripts are evaluated first and then run immediately.
// contents of functions are not evaluated until the function is called
// This will run immediately after the whole script has been evaluated
console.log('Is thisWillRunRightAwayLuckilyItWasEvaluatedFirst set already? ' + (undefined !== thisWillRunRightAwayLuckilyItWasEvaluatedFirst));
// Since the whole script was evaluated first, this already exists and can be run now
thisWillRunRightAwayLuckilyItWasEvaluatedFirst();
// This will run next
console.log('Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt set already? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// Okay, it's not defined yet. That's probably because evaluation doesn't process assignments yet
// This is the traditional way of declaring a function. It is evaluated once the script loads
function thisWillRunRightAwayLuckilyItWasEvaluatedFirst(){
console.log('This function is evaluated when the script loads. It can be run even before this declaration is reached.');
}
// This is another way of declaring a function, however, the only thing evaluated when
// the script loads is that we declared the variable. It did not actually assign the function
// to the variable yet
var thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt = function(){
console.log('We can\'t run this until after this point, since it has not actually been assigned yet.');
};
// This will run
console.log('How about now? Is thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt defined yet? ' + (undefined !== thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt));
// It is clear that the function is now defined and can be run
thisIsDeclaredAlreadyButNotActuallySetYetSoWeCantRunIt();
// After JS evaluates the script, it will do something referred to as "hoisting"
// More on this later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment