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
<!-- Embed code directly on a web page using script tags. --> | |
<script> | |
alert( "Hello World!" ); | |
</script> |
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
<!-- Inline code directly on HTML elements being clicked. --> | |
<a href="javascript:alert( 'Hello World' );">Click Me!</a> | |
<button onClick="alert( 'Good Bye World' );">Click Me Too!</button> |
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
<!doctype html> | |
<html> | |
<head> | |
<script> | |
// Attempting to access an element too early will have unexpected results. | |
var title = document.getElementById( "hello-world" ); | |
console.log( title ); | |
</script> | |
</head> | |
<body> |
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
// Creating an object with the constructor: | |
var person1 = new Object; | |
person1.firstName = "John"; | |
person1.lastName = "Doe"; | |
alert( person1.firstName + " " + person1.lastName ); | |
// Creating an object with the object literal syntax: | |
var person2 = { |
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 foo = []; | |
foo.push( "a" ); | |
foo.push( "b" ); | |
alert( foo[ 0 ] ); // a | |
alert( foo[ 1 ] ); // b | |
alert( foo.length ); // 2 | |
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" |
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
// 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
// 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
// Flow control | |
var foo = true; | |
var bar = false; | |
if ( bar ) { | |
// This code will never run. | |
console.log( "hello!" ); | |
} | |