console.log('hello world!');var variable;var integer = 1;var float = 1.3;var bool = truevar string = 'monkey';
var a = 1 + 1; // 2var b = 4 - 2; // 2var c = 2 * 2; // 4var d = 8 / 4; // 2var e = true || false; // truevar f = true && false; // falsevar g = !true; // falsevar h = (1 + 2) * 3; // 9var i = 1 == 1; // truevar j = 2 != 3; // true
function add(a, b) {return a + b;}var add = function (a, b) {return a + b;};
var displayClosure = function() {var count = 0;return function () {return ++count;};};var inc = displayClosure();inc(); // returns 1inc(); // returns 2inc(); // returns 3
var arr = [1, 2, 3, 4];arr[2]; // 3var mixedArr = [1, 1.3, 'monkey'];arr.push(5); // [1, 2, 3, 4, 5]
var obj = {key: 'value',otherKey: ['some', 'other', 'values'],method: function() {return this.key + ' it is';}};obj.key; // 'value'obj['otherKey']; // ['some', 'other', 'values']obj.method(); // 'value it is'
var a = 1;if (a == 1) {console.log('a is one')}if (a == 2) {console.log('wow such two :(');} else {console.log('wow so not two :(');}
var i = 10;if (i < 5) { console.log('smaller than 5');} else if (i < 7) {console.log('bigger than 4 and smaller than 7');} else {console.log('bigger than 6');}
var a = 0;while (a < 5) {a += 1;console.log(a);}
for (var i = 0; i < 7; i++) {console.log(i);}
var arr = ['wow', 'so', 'very', 'such', 'array', 'wow'];for (var i = 0; i < arr.length; i++) {console.log(i);}
its just an other programming language
for (var i = 0; i < 7; ++i) {var a = i + 3;}console.log(a); // 10
function some() {var a = 4;return a;}console.log(a); // undefined
for (var i = 0; i < 7; ++i) {(function (index) {var a = index + 3;})(i)}console.log(a);
function addHandlers(nodes) {for (var i = 0; i < nodes.length; i += 1) {nodes[i].onclick = function (e) {alert(i);};}}
function addHandlers(nodes) {for (var i = 0; i < nodes.length; i += 1) {nodes[i].onclick = (function(i) {return function (e) {alert(i);}})(i);}}
function three(a, b, c) {console.log(a, b, c);}three(1); // 1 undefined undefinedthree(1, 2, 3, 4); // 1 2 3
function increment(a) {a++;console.log(a);}var b = 1;increment(b) // 2console.log(b);
function incrementArr(arr) {arr[0]++;}var a = [1, 2];incrementArr(a);console.log(a); // [2, 2]
function incrementObj(obj) {obj['monkey']++;}var o = {monkey: 1};incrementArr(o);console.log(o); // {monkey: 2}
function sum() {var i, sum = 0;for (i = 0; i < arguments.length; ++i) {sum += arguments[i];}return sum;};console.log(sum(4, 8, 15, 16, 23, 42)); // 108
var obj = {property: 1,getProperty: function() {return this.property;}}
function test(a, b) {console.log(this, a, b);}test(1, 2); // window 1 2test.call(obj, 1, 2); // obj 1 2test.apply(obj, [1, 2]); // obj 1 2
var obj1 = {};var obj2 = new Object();
var obj = {apple: 1};function Constructor(p) {this.apple = 1;this.pear = p;}function factory(p) {self = {};self.apple = 1;self.pear = p;return self;}var newObj1 = new Constructor(2);var newObj2 = Object.create(obj, {pear: {value: 2}});var newObj3 = factory(2);
function Constructor(a) {this.apple = a;}Constructor.prototype = {pear: 2};var obj = new Constructor(1);console.log(obj.apple); // 1console.log(obj.pear); // 2obj.pear = 3;console.log(obj.pear); // 3
function Point(x, y) {this.x = x;this.y = y;}Point.prototype.getSize = function() {return Math.sqrt(this.x * this.x + this.y * this.y);}var point = new Point(3, 4);point.getSize(); // 5