We are kicking off from where we left off in Part 1 of the new features of ES6.
String and Number Methods
There are quite a few new features of strings and string methods available in ES6. These are used to target expressions inside of a string. We are going to be dealing with the following:
Let us have an example of how it works.
In the snippet above, we have made use of one of the three new methods ES6 providesâ€Š–â€ŠstartsWith(). What this method does is that it checks if the string value supplied starts with a matching string which is usually a string inside of the brackets. So, we are checking if the fullString variable starts with Hello. This method returns a Boolean so if it is the same, it returns true else, returns false.
Note: It is case sensitive. This means that â€˜hello’ is different from â€˜Hello’.
In the second example above, we can see that while using the second methodâ€Š–â€ŠendsWith(), we are checking if the value of the string ends with a particle text or value. You should already know that even if our argument was Games, it returned false. This is because like I said earlier, they are case sensitive so we expect our result to be false.
In the third example above, we can see that while using the third methodâ€Š–â€Šincludes(), we are checking if the value of the string passed is in the main string at any position. Since our string William can be found in the fullString, we get a true value returned.
We can also do some number manipulations in ES6 such as binary, octal etc.
Let us take a look at Number functions.
There are quite some more number methods which you could check up on your own and practice more.
Default Parameters and Spread Operators.
Default parameters in ES6 works pretty much the same way as in PHP and all. When we want a default value to be used say when we run a function or something, all we have to do is assign it in the parameter where we passed it in. Here is a look at an example.
Rather than having an undefined as the output, we can set it to hold or have a default parameter which makes it look nicer rather than the default undefined we get. Here is a look at that.
All we did to get that output was to set a default parameter inside of our function. If we were to that from ES5, we would probably have something like this.
You can confirm that with ES6, it is pretty much lesser code to write.
Let us take a look at the Spread Operators.
The spread operator is represented with three dots (…) and it is used to allow an expression to be expanded in places where multiple arguments are expected. Let us take a quick look at that. In ES5, if we want to have an array passed in as a parameter to the function, we would be making use of the apply method as shown below.
However, in ES6, we do not need to make use of the apply method. All we need is to make use of the three dots as shown below.
This gives the same output as the earlier one but with much lesser code to write. Nice yeah?
Let us look at having two variables and using our arguments passed in.
Enjoying the new features I guess.
Map and Set Data Structure.
The set objects allow us to store unique values of any type. Can be primitive values or object references.
Let us take a look at how this works.
Consider the snippet above. We created an array called myArr. We then created an object of the Set method and passed in the array we created and finally logged the object to the console. We can see that in the output section, the set even tells us what kind of element is contained within which happens to be the array we passed in. Let us try to do some manipulation to the Set object created.
If we take a good look at what we tried to achieve, we added a string value to the set and also an object to the set. We also removed an element from the set. You can see that from the two outputs generated above, we see what type of value is stored in the setâ€Š–â€ŠArray, String and Object.
You can also tryout methods like this on your own:
mySet.clear(); // This clears the entire set
mySet.size; // This returns the size of the set object.
There are other methods you can check up to familiarize yourself with set.
Let us try to perform looping with the set object.
We can see that in our output, our set object looped through each element in the set list and logged the result to our console.
Let us dive into Maps.
Maps are key value pairs as opposed to Set which are individual values. Let us take a look at an example.
In the example above, we created a simple map object with an array and each key (a1, b1) having its own value (Hello, How are you) respectively. We can also add elements or other key value pairs to the object. Let us take a look at that.
There are also other methods that can be used with the map object.
Some include: Clear, delete, forEach, has, size etc.
To delete, we make use of the key. Let us take a look at that.
In addition to Set and Map, we have WeakSet and WeakMap. These have to do with objects. Let us take a look at how that works.
We created an object of Weakset and called it carWeakSet. We then created an object called firstCar with its properties been make and model. We then used the add method to add the firstCar object to the carWeakSet object as seen above.
Let us try an example with the WeakMap.
We can see that the key created also has a value assigned to it. The method from the Set and Map can be applied to the WeakSet and WeakMap respectively.
Stay tuned for the final part on the new features of ES6.
Thanks for reading.