Javascript ========== Javascript Guide on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide Expressions and Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators ES6 Vs ES5 http://es6-features.org/#Constants Handy code ------------ .. code-block:: javascript // to execute code in the Web Browser Console (function(){ "use strict"; /* Start of your code */ function greetMe(yourName) { alert('Hello ' + yourName); } greetMe('World'); /* End of your code */ })(); // type of operator console.log(typeof alert); console.log(typeof Object()); Structure --------- Variable Declarations * ``var`` global variable * ``let`` block-scoped local variables * ``const`` block-scoped read-only named constant .. code-block:: javascript // multiple declarations on a line let user = 'John', age = 25, message = 'Hello' // constants const PI = 3.14 if (true) { let y = 5; } console.log(y); // ReferenceError: y is not defined data = {a: "apples", b: "book"} //element access data['b'] // or data.b let keys = Object.keys(data) let values = Object.values(data) // Destructuring Assignment // define a variable in the local scope named b, whose value is data[b] let {b} = data // Here's another interesting unpacking syntax let [name, age] = ['Mika', 28]; //undefined, which is a keyword, and is testable var input; if (input === undefined) { //do something } //undefined is also Boolean false if (!input){ console.log("variable is not yet defined") } Variable and function Hoisting Variables defined and declared at the bottom of the script are initially parsed before The exeuction of the script, so they can be used in a statement before their declaration. Hoisted Varables are always undefined Hoister functions work Hoisted function expression (function object assigned to a variable) doesn't https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variable_hoisting Datatypes ---------- * null * undefined * Boolean: literals ``true`` and ``false`` * String * Number: including integers and floats * BigInt: an integer with arbitrary precision * Symbol: A data type whose instances are unique and immutable. * Object .. code-block:: javascript // Strings and formatted strings let str = "Hello" let str2 = 'Single quotes are ok too' let phrase = `can embed another ${str}` // string concat let s = "my" + "string" // one one type of numbers let n1 = 10, n2 = 3.14 typeof n1 typeof n2 Datatype Conversions ^^^^^^^^^^^^^^^^^^^^ **Converting String to Numbers** * parseFloat() * parseInt() .. code-block:: javascript let secret = 3.14 let data = 'the secret is ' + secret // or data = `the secret is ${secret}` // number to string conversion happens automtically // parse a binary number parseInt("1111",2) // parse a hexa number parseInt("FF",16) literals ^^^^^^^^ .. code-block:: javascript // array syntax let myarray = ['alpha', 'beta', 'gamma'] // array literal with missing gaps let myarray = ['a','b',,,'e','f'] let num = 0xFFFF; //65K in decimal // object literals, nested // object keys can be numbers var car = { manyCars: {a: 'Saab', b: 'Jeep'}, 7: 'Mazda' }; console.log(car.manyCars.b); // Jeep console.log(car[7]); // Mazda // regexp literal var re = /ab+c/; Control Flow and Error Handling ------------------------------- .. include:: controlflow.rst Looping and Interation ---------------------- * ``for`` statement * ``do .. while`` statement * ``while`` statement * labeled statement * ``break`` statement * ``continue`` statement * ``for .. in`` statement * ``for .. of`` statetment The classical ``for`` statement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: javascript for (let step = 0; step < 5; step++) { // Runs 5 times, with values of step 0 through 4. console.log('Walking east one step'); } ``do while`` statement ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: javascript let i = 0; do { i += 1; console.log(i); } while (i < 5); ``while`` statement ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: javascript let n = 0; let x = 0; while (n < 3) { n++; x += n; } labeled and ``break`` and ``continue`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: javascript let x = 0; let z = 0; labelCancelLoops: while (true) { console.log('Outer loops: ' + x); x += 1; z = 1; while (true) { console.log('Inner loops: ' + z); z += 1; if (z === 10 && x === 10) { break labelCancelLoops; } else if (z === 10) { break; } } } let i = 0; let j = 10; checkiandj: while (i < 4) { console.log(i); i += 1; checkj: while (j > 4) { console.log(j); j -= 1; if ((j % 2) === 0) { continue checkj; } console.log(j + ' is odd.'); } console.log('i = ' + i); console.log('j = ' + j); } ``for in`` statement ^^^^^^^^^^^^^^^^^^^^^^^ Iterates over Object Property Names. .. warning:: do not use ``for in`` or ``for of`` or use with Arrays, because it is slower, and more buggy with Arrays. Use Array.forEach and Array.map instead Works like Python for Objects, but working nothing like python for arrays. .. code-block:: javascript let data = {a:'apples',b:'book',c:'cat'} // the keys of an object, and not the values for (item in data){ console.log(`I eat ${data[item]}`) } } ``for of`` statement ^^^^^^^^^^^^^^^^^^^^^ .. warning:: do not use ``for in`` or ``for of`` or use with Arrays, because it is slower, and more buggy with Arrays. Use Array.forEach and Array.map instead .. code-block:: javascript // To iterate over object values let data = {a:'apples',b:'book',c:'cat'} for (item of Object.values(data)){ console.log(`I eat ${item}`) } Functions --------- .. include:: functions.rst Indexed Collections -------------------- .. include:: arrays.rst Keyed Collections ------------------ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections * Map * WeakMap * Set * WeakSet .. code-block:: javascript // Map let sayings = new Map(); sayings.set('dog', 'woof'); sayings.set('cat', 'meow'); sayings.set('elephant', 'toot'); sayings.size; // 3 sayings.get('dog'); // woof sayings.get('fox'); // undefined sayings.has('bird'); // false sayings.delete('dog'); sayings.has('dog'); // false for (let [key, value] of sayings) { console.log(key + ' goes ' + value); } // "cat goes meow" // "elephant goes toot" sayings.clear(); sayings.size; // 0 // Set let mySet = new Set(); mySet.add(1); mySet.add('some text'); mySet.add('foo'); mySet.has(1); // true mySet.delete('foo'); mySet.size; // 2 for (let item of mySet) console.log(item); Objects ------- .. include:: objects.rst Expressions and Operators ------------------------- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators Promises -------- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises Iterators and generators ------------------------ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators Node Linux Process ------------------ .. code-block:: javascript //variable args let args = process.argv console.log(args) //discard the first two elements of the array args = process.argv.slice(2) // discarding the exec and the script path //the process object has losts of useful info about the OS process console.log(process)