Friday, 31 July 2015

Javascript - “let” keyword vs “var” keyword

The difference is scoping. var is scoped to the nearest function block (or global if outside a function block), and let is scoped to the nearest enclosing block (or global if outside any block), which can be smaller than a function block.

Global:

They are identical when used like this outside a function block.

let me = 'go'; //globally scoped
var i = 'able'; //globally scoped
  

Function:

They are identical when used like this in a function block.

function ingWithinEstablishedParameters() {
    let terOfRecommendation = 'awesome worker!'; //function block scoped
    var sityCheerleading = 'go!'; //function block scoped
};
  

Block

Here is the difference. let is only visible in the for() loop and var is visible to the whole function.

function allyIlliterate() {
    //tuce is *not* visible out here

    for( let tuce = 0; tuce < 5; tuce++ ) {
        //tuce is only visible in here (and in the for() parentheses)
    };

    //tuce is *not* visible out here
};

function byE40() {
    //nish *is* visible out here

    for( var nish = 0; nish < 5; nish++ ) {
        //nish is visible to the whole function
    };

    //nish *is* visible out here
};
  

Wednesday, 29 July 2015

How to get flags of RegEx in ES6

In ES5

it’s possible to get the text of the regular expression by using the source property, but to get the flag string requires parsing of toString(), such as:

 function getFlags(re) {
     var text = re.toString();
     return text.substring(text.lastIndexOf("/") + 1, text.length);
 }

 // toString() is "/ab/g"
 var re = /ab/g;

 console.log(getFlags(re));          // "g"
 

In ES6

 var re = /ab/g;

 console.log(re.source);     // "ab"
 console.log(re.flags);      // "g"
 

New Function in ES6

repeat()

console.log("x".repeat(3));         // "xxx"
console.log("hello".repeat(2));     // "hellohello"


Object.is()

When you want to compare two values, you’re probably used to using either the equals operator (==) or the identically equals operator (===). Many prefer to use the latter to avoid type coercion during the comparison. However, even the identically equals operator isn’t entirely accurate. For example, the values +0 and -0 are considered equal by === even though they are represented differently in the JavaScript engine. Also NaN === NaN returns false, which necessitates using isNaN() to detect NaN properly.

In many cases, Object.is() works the same as ===. The only differences are that +0 and -0 are considered not equivalent and NaN is considered equivalent to NaN. Here are some examples:

console.log(+0 == -0);              // true
console.log(+0 === -0);             // true
console.log(Object.is(+0, -0));     // false

console.log(NaN == NaN);            // false
console.log(NaN === NaN);           // false
console.log(Object.is(NaN, NaN));   // true

console.log(5 == 5);                // true
console.log(5 == "5");              // true
console.log(5 === 5);               // true
console.log(5 === "5");             // false
console.log(Object.is(5, 5));       // true
console.log(Object.is(5, "5"));     // false




How can I check if one string contains another substring?

In ES5

indexOf returns the position of the string in the other string. If not found, it will return -1:

    var s = "foo";
    alert(s.indexOf("oo") > -1);
 

In ES6 there are three new methods includes(), startsWith(), endsWith()

 var msg = "Hello world!";

 console.log(msg.startsWith("Hello"));       // true
 console.log(msg.endsWith("!"));             // true
 console.log(msg.includes("o"));             // true

 console.log(msg.startsWith("o", 4));        // true
 console.log(msg.endsWith("o", 8));          // true
 console.log(msg.includes("o", 8));          // false