I’m going to discuss each topic sequentially. Let’s begin…..
includes() method is used to find whether one string may be found within another string or not. It returns a boolean value true or false. The syntax is given below —
From the syntax of
includes() method, we can see that it takes two parameters. The first parameter is searchString which represents the string which we will search in the string to find out whether it exists in that particular string or not. The second parameter represents the position from where the search will be processed. It is an optional parameter. If we don’t provide it then the search will be taken place from the starting index. Let’s see the demo of how the
includes() method works —
As the second parameter isn’t given, the search will be taken place from the starting index. And here the search string is “gold” so it will search for gold. Since, it is present in the string, it will return a true.
One thing you need to keep it in your mind is that the
includes() method is case sensitive. It doesn’t treat lowercase characters the same way as uppercase characters. Let’s see the example of what happens if we use “Gold” instead of “gold” as a search string.
Here, “Gold” is used instead of “gold”, so it will return a false.
substr() method returns a portion of a string. It takes two parameters where the first one represents the index of the first character to include in the returned string and the second one represents the number of characters to take out from the string and it is optional.
The syntax is given below —
Since the first parameter is 1 so the first character of the returned string will be a because index counts starts with 0. The returned string contains three characters because the second parameter is 3.
Now you might be thinking of what will happen if I don’t put the value of the second parameter as it’s optional. Well, if you don’t give any value as the second parameter then it will take out the rest character of the given string. The demo is given below —
parseInt() function is used to accept the string and radix/base as parameters then converted it into an integer. The second parameter radix is optional and is used to specify which numerical system to be used as the first parameter. If the radix parameter isn’t given then the
parseInt() function treats the given string parameter as a decimal value.
parseInt() method converts floating number string into decimal integer value. Let’s see an example —
If the string doesn’t contain a number, it returns NaN as the string isn’t a number. The demo is shown in the following image below -
Through this method, we can easily convert binary, octal, and hexadecimal numbers into a decimal number.
Let’s do convert binary number into a decimal number.
The picture above shows the string given “11” is converted into decimal number 3. That’s very easy to convert, isn’t that? Now one question may come to your mind is that what if the string doesn’t consist of binary numbers then what will be the output? Well, the answer to the question is that the output will be NaN. Let’s see this -
The way we can convert binary numbers into a decimal number the same we can convert octal and hexadecimal numbers into decimal numbers.
every() method test whether all elements in an array doesn’t the test implemented by the provided function or not. It returns boolean value. If all elements pass the test then it returns true, otherwise false.
In the above picture test all elements in the array whether it is below thirty or not. Then it found that all elements in the array pass the test so it returns true as an output.
What if only one element in the array doesn't pass the test?
The answer to the question is that the output will be false. All elements in an array have to pass the test implemented by the function to get true as an output.
From the picture shown above, we can see that one element in an array doesn’t pass the test function, the value of an element is below thirty is not, the last element of the given array is 35 which is greater than 30. Since all elements don’t pass so the output is false.
One thing we have to remember while using this
every() method is that if we call this method on an empty array it will return true for any condition. The demo is given below —
The output is true as we call this method on an empty array.
forEach() is a method that executes a provided call back function once for each array element in an ascending order. There is no way to stop or break a
forEach() loop other than by throwing an exception.
Since the elements in an array is 3, it invokes the call back function 3 times. The call back function isn’t called for index properties that have been deleted or are uninitialized.
The missing value between Banana and Orange didn’t invoke the callback function as there is no operation for uninitialized value in
Object.freeze() method freeze an object and this frozen object can no longer be changed. It returns the same object as it was before the freeze. We can’t add or delete any property of a frozen object. Let’s see the demo to more clear —
At first we declared an object, name person, with a property age and then freeze the object. After freezing the object, we have tried to change the value of property age and also tried to add a new property “name”. Since it is frozen so it returns the same object.
Object.seal() method seals an object. It works quite same like
Object.freeze() . Like
Object.freeze(), it also prevents add new and delete new properties. But one thing is different from
Object.freeze() is that the values of present properties can still be changed as long as they are writable. Let’s see the demo -
At first we declared an object, name person, with a property age and then seals the object. After that, we have tried to change the value of property age and also tried to add a new property “name”. Since it is sealed so it returns with a changeable value of the age properties that we can see as expected output.
The split method divides a string into an ordered set of substring, puts this substring into an array, and then returns it. Let’s see how we can use it —
The picture above shown we have taken a string, called str to split it. We haven’t used any separator to split the string which is why it returns the same string into an array. Now you might be wondering about what would happen if we use a separator to split the string. Let’s see the demo -
If we use a whitespace as a separator, then the string will be split with the whitespace in a string that we can in expected output.
We can also use limit with separator. Let’s see what the output is if we do so —
As we have set the limit to 3 so it returns the first three split value of the string.
concat() method is used to merge two or more arrays. This method returns a new array. It has no impact on the existing array as it returns a new array. Let’s see the demo —
If you want to concatenate more than two arrays, you can do so. Let me show you how you can do it -
Math.log() function returns the natural logarithm of a number. It is equivalent to ln(x) in mathematics. If the number is negative, NaN is returned. If the number is 0, the return is -infinity. Let’s see the demo -
That’s all for today.