Signup/Sign In

The only 2 new features of the new Javascript ES7 or ECMAScript 2016

Posted in Programming   LAST UPDATED: OCTOBER 22, 2017

    All the Web Developers love Javascript. Javascript is a language which has inspired hundreds of frameworks which are built on the top of it. Popular frameworks like Backbone JS, Node JS, Angular JS etc, all have a lot of things common with the core Javascript language. For those who are new to Javascript, it is a dynamic programming language, which when used with HTML, can alter the DOM (webpage structure) at runtime, in simpler words, if you want to hide any HTML element, or maybe change the size of any element at runtime, or perform some operation when a user clicks on any button, all this can be easily accomplished using Javascript.

    In this article, we will discuss the new features added as standards to the ECMAScript in June 2016 in the version ECMAScript 2016. This time only 2 simple new features were added, which made this release a very small update as developers were doing just fine even without the new features.

    So let's see what these new features are:




    1. Array.prototype.includes()

    To find out whether an element exists in an array or not, what would you do? As of now, we used to use .indexOf() function, which returns the index of the element in the array if present, else it returns -1.

    // using indexOf() to find the index at which an element exists
    
    ['i', 'love', 'optimus', 'prime'].indexOf('love')  // 1

    To find out if an element exists in an array or not, the ideal way should be to have a function which returns True if the element exists and False if the element doesn't but as of now we were using .indexOf() for this purpose, but not anymore.

    The new .includes() function returns True if an element exists in an array, and returns False otherwise.

    var coolCities = ['New York', 'New Delhi', 'Sydney']
    
    coolCities.includes('Sydney')     // true
    
    coolCities.includes('Karachi')    // false, no offence

    Syntax:

    Array.prototype.includes( searchElement [ , fromIndex ] )

    searchElement - The element that you want to search.

    fromIndex (optional) - the index from where you want to search for the element in the array

    The .includes() method has some good enhancements over the .indexOf() method, here are a few with code examples:

    [NaN].indexOf(NaN)    // -1 , this does not work
    
    [NaN].includes(NaN)    // true, works just fine
    
    [1,  , 3].indexOf(undefined)    // -1, this too does not work
    
    [1,  , 3].includes(undefined)   // true, includes understands undefined

    One more important fact about the .includes() method is that it searches in the ascending order, i.e. it starts from the beginning of the array.




    2. The Exponential Operator (**)

    This one is quite similar to the already popular ** operator in languages like Python etc, used for exponential operations. This operator works exactly like Math.pow()

    x**y will return the result of x raised to the power y, for example:

    2**2 // 4
    
    2**'string' //NaN

    And that's it, you now have upgraded your skill as per the latest ES7 release. In our next article, we will cover the ES8 features which were released in June 2017.

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    Tags:ECMAScript2016ES7JavaScript
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS