Created
September 3, 2015 12:52
-
-
Save siakaramalegos/32e895025038aa0f17a1 to your computer and use it in GitHub Desktop.
Practice JavaScript concepts
This file contains 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 message = "hello world"; | |
// console.log(message); | |
// var x = "hey"; | |
// var y = 10; | |
// console.log(x); | |
// console.log(y); | |
// console.log(x + y); | |
// y = " is for horses"; | |
// console.log(x + y); | |
// var x = 1, | |
// y = 2, | |
// z = "3"; | |
// console.log(x + y + z); | |
// Numbers | |
// var integer_number = 1, | |
// float_number = 1.23; | |
// // console.log(integer_number + float_number); | |
// // // Booleans | |
// var is_cool = true; | |
// // console.log(is_cool + " dat") | |
// // Arrays | |
// var my_stuff = [ | |
// integer_number, | |
// float_number, | |
// is_cool | |
// ]; | |
// console.log(my_stuff); | |
// Hashes | |
// var capitals = { | |
// LA: "Baton Rouge", | |
// TX: "Austin", | |
// GA: "Atlanta" | |
// }; | |
// console.log(capitals["LA"]); | |
// Null and undefined | |
// var x; | |
// Variables that haven't been initialized are undefined. | |
// console.log(x); // undefined | |
// But they aren't null | |
// console.log(x === null); // false | |
// Unless you actually make them null | |
// x = null; | |
// console.log(x); // null | |
// console.log(x === null); // true | |
// console.log(x === undefined); // false | |
// OR, use 'type coercion'... 2 equal signs in JS will change the value | |
// rather than simply compare. So BE CAREFUL! CUIDADO! PROSEXE! | |
// console.log(x == undefined); // true | |
// This variable is in the Global scope | |
// var x = "I'm a global variable called x!"; | |
// console.log(x); | |
// // Define a function called someFunction | |
// function someFunction(){ | |
// // This variable only exists inside the function (it's local) | |
// var y = "I'm a local variable called y!"; | |
// console.log(x); | |
// console.log(y); | |
// // this is bad. don't do it. | |
// x = "I'm now a BAD local variable called x" | |
// console.log(x); | |
// // this is automatically global - don't do it! always use var for declaring new variables. | |
// z = 10; | |
// console.log(z); | |
// }; | |
// someFunction(); | |
// console.log(x); | |
// console.log(z); | |
// Operators | |
// var x = 10, | |
// y = 5; | |
// console.log(x + y); | |
// console.log(x - y); | |
// console.log(x * y); | |
// console.log(x / y); | |
// console.log(x % y); //modulus, or remainder | |
// console.log(x > y); // true | |
// console.log(x < y); //false | |
// console.log(x >= y); //true | |
// console.log(x <= y); //false | |
// console.log(x === y); // false | |
// console.log(x !== y); //true | |
// var a = true, | |
// b = false; | |
// console.log(a && b); // and... false | |
// console.log(a || b); // or... true | |
// Conditionals (if statements) | |
// var x = 10, | |
// y = "ten"; | |
// if (x > y){ | |
// console.log("x is greater than y"); | |
// } else if (x < y){ | |
// console.log("x is less than y"); | |
// } else { | |
// console.log("x is neither less than nor greater than y"); | |
// } | |
// Loops | |
// C-style JavaScript loop | |
// var x = [1, 2, 3, 4, 5]; | |
// for (var i = 0; i < x.length; i++){ | |
// console.log(x[i]); | |
// } | |
// For each loop | |
// x.forEach(function (element){ | |
// console.log(element); | |
// }); | |
// var i = 0; | |
// while (i < x.length){ | |
// console.log(x[i]); | |
// i++ // same as i += 1 | |
// } | |
// Functions | |
// a function with explicit arguments | |
function sumExplicitly(a, b){ | |
return a + b; | |
} | |
var sum = sumExplicitly(4, 5); // 9 | |
console.log(sum); | |
// a function with implicit arguments | |
// function sumImplicitly(){ | |
// var total = 0; | |
// for (var i = 0; i < arguments.length; i++){ | |
// total -= arguments[i]; | |
// } | |
// console.log(total); | |
// } | |
// sumImplicitly(1, 2, 3, 4, 6, 125, 231241); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment