This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
// Even though the condition evaluates to false | |
// this loop's body will still execute once. | |
alert( "Hi there!" ); | |
} while ( false ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = 0; | |
while ( i < 100 ) { | |
// This block will be executed 100 times. | |
console.log( "Currently at " + i ); | |
i++; // Increment i | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for ( var i = 0; i < 5; i++ ) { | |
// Logs "try 0", "try 1", ..., "try 4". | |
console.log( "try " + i ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stuffToDo = { | |
"bar": function() { | |
alert( "the value was bar -- yay!" ); | |
}, | |
"baz": function() { | |
alert( "boo baz :(" ); | |
}, | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A switch statement | |
switch ( foo ) { | |
case "bar": | |
alert( "the value was bar -- yay!" ); | |
break; | |
case "baz": | |
alert( "boo baz :(" ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Flow control | |
var foo = true; | |
var bar = false; | |
if ( bar ) { | |
// This code will never run. | |
console.log( "hello!" ); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Comparison operators | |
var foo = 1; | |
var bar = 0; | |
var baz = "1"; | |
var bim = 2; | |
foo == bar; // false | |
foo != bar; // true | |
foo == baz; // true; but note that the types are different |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do something with foo if foo is truthy. | |
foo && doSomething( foo ); | |
// Set bar to baz if baz is truthy; | |
// otherwise, set it to the return value of createBar() | |
var bar = baz || createBar(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Multiplication and division | |
2 * 3; | |
2 / 3; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Concatenation | |
var foo = "hello"; | |
var bar = "world"; | |
console.log( foo + " " + bar ); // "hello world" |