var myVariableName = 'myValue';
var likesCake = true;
var age = 22;
var PI = 3.14159;
var name = 'Sam';
var job = "Developer";
var add = function (a, b) { return a * b; };
var person = { name: "Josh", age: 19, isAwesome: true };
var myValues = [45, 'foo', true];
myValues.length; // returns 3
myValues.push('bar'); // adds bar to the array
myValues[1]; // returns 'foo'
var importantThing; console.log(importantThing); // returns undefined
var moreData = null;
Equals | age === 10 |
Not Equal To | age !== 10 |
Less | age < 10 |
Less or Equal | age <= 10 |
Greater | age > 10 |
Greater or Equal | age >= 10 |
Not | !foo |
And | foo && bar |
Or | foo || age < 10 |
if (value) { // to do if value is true } else { // to do if value is false }
switch (state) { case "Utah": // do stuff break; case "California": // do stuff break; default: // do stuff }
for (var i = 0; i < 5; i++) { console.log(i); } // prints out 01234
var i = 0; while (i < 5) { console.log(i); i++; } // prints out 01234
var i = 0; do { console.log(i); i++; } while (i < 5); // prints out 01234
break; // exits out of the loop
continue; // skips to the next loop iteration
var dog = { name: 'spot', age: 4, bark: function () { console.log('arf!'); }, children: ['scout', 'fido'], "favorite-things": { toy: 'bone', food: 'ravioli' } };
var dog = { ... }; dog.name // returns 'spot' dog.age // returns 4 dog['age'] // returns 4 dog.bark() // runs that function dog.children[0] // returns 'scout' dog['favorite-things'].person = 'dallin' // adds a new property to the object
function add(a, b) { return a + b; }
var add = function (a, b) { return a + b; };
var myFunc = function (a) { if (a) { console.log('parameter passed'); } else { console.log('parameter not found'); } } console.log(myFunc()); // displays 'parameter not found' // displays 'undefined'
var doSomething = function () { // do something awesome return a; }
(function (a) { console.log(a); }('hey there')); // displays 'hey there'
(function (a) { console.log(a); })("what's up"); // displays "what's up"
document.getElementById(id); // grabs an element from the DOM
instanceof age instanceof Number; // returns true; // determines if a variable is an instance of a certain type.
typeof typeof age; // returns Number // determines what type a variable is
parseInt(val) parseInt("34"); // returns 34 // converts a string into a number
Thanks for watching!