function(){ return 1 + 2 }
➙
function(){ return; 1 + 2; }
➙ always use ;
if (true) doThis() doThat()
➙
if (true) { doThis(); } doThat();
var num = 42; var num2 = x * 42; var str = "BioJS"; var arr = ["BioJS", "is", "aweseome"]; var dict = {name:"BioJS", url:"www.biojs.net"};
Access
JavaScript Object Notation
"course": { "chair": "RostLab", teachers: ["Tatyana Goldberg", "Juan Miguel Cejuela"], "location": { "postcode": "85748", "city": "Garching" } }
Value Class Type ------------------------------------- "foo" String string new String("foo") String object 1.2 Number number new Number(1.2) Number object true Boolean boolean new Boolean(true) Boolean object new Date() Date object new Error() Error object [1,2,3] Array object new Array(1, 2, 3) Array object new Function("") Function function /abc/g RegExp object (function in Nitro/V8) new RegExp("meow") RegExp object (function in Nitro/V8) {} Object object new Object() Object object
'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true true == '1' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true
== is weakly-typed and slower than ===
Simple for loop
for(var i = 0; i < arr.length; i++){ console.log(arr[i]); }
forEach
arr.forEach(function(entry){ console.log(entry); });
for-in
for(var key in arr){ console.log(arr[key]); }
for-in
if (arr.hasOwnProperty(key){ }
method.bind(instance);
CS / ES6: =>
function(){ var self = this; foo.on("click", function(evt){ console.log(this,self); } }
x = 5; // Assign 5 to x elem = document.getElementById("demo"); // Find an element elem.innerHTML = x; // Display x in the element var x; // Declare x
(only for var declarations)
© yuiblog.com