Skip to content

Instantly share code, notes, and snippets.

View carlosrojaso's full-sized avatar
🎯
Focusing

Carlos Rojas carlosrojaso

🎯
Focusing
View GitHub Profile
@carlosrojaso
carlosrojaso / Inline
Created May 14, 2014 20:52
JS Inline
<!-- Embed code directly on a web page using script tags. -->
<script>
alert( "Hello World!" );
</script>
@carlosrojaso
carlosrojaso / jsAttributes
Created May 14, 2014 20:53
js Attributes
<!-- 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>
@carlosrojaso
carlosrojaso / JSPlacement
Created May 14, 2014 20:56
JS Placement
<!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>
// 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 = {
var foo = [];
foo.push( "a" );
foo.push( "b" );
alert( foo[ 0 ] ); // a
alert( foo[ 1 ] ); // b
alert( foo.length ); // 2
@carlosrojaso
carlosrojaso / jsConcatenation
Created May 14, 2014 21:07
JS Concatenation
// Concatenation
var foo = "hello";
var bar = "world";
console.log( foo + " " + bar ); // "hello world"
// Multiplication and division
2 * 3;
2 / 3;
// 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();
@carlosrojaso
carlosrojaso / JSComparison
Created May 14, 2014 21:11
JS Comparison operators
// 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
// Flow control
var foo = true;
var bar = false;
if ( bar ) {
// This code will never run.
console.log( "hello!" );
}