Hey reader👋 Hope you are doing well😊
In the last post we have talked about about some pre-defined objects in JavaScript. In this post we are going to know about Math library, RegEx and destructuring.
So let's get started🔥
JavaScript Math Object
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Note that JavaScript Math object is static i.e. we don't need to create the math object first to get access to Math's properties and methods.
Math Properties
Math Methods
- Math.round(n) -> returns the nearest integer.
- Math.random() -> returns a random number between 0 (inclusive), and 1 (exclusive).
- Math.floor(n) -> returns the value of number rounded down to its nearest integer.
- Math.ceil(n) -> returns the value of number rounded up to its nearest integer.
- Math.trunc(n) -> returns the integer part of number.
- Math.min(a,b,c,d) -> return the minimum number among given numbers.
- Math.max(a,b,c,d) -> return the maximum number among given numbers.
- Math.pow(a,b) -> return a raised to power b.
- Math.sqrt(a) -> return the square root of a number.
- Math.abs(a) -> return the absolute value of given number.
- Math.sign(a) -> return 1 if number is positive, -1 if negative and 0 in other cases.
- Math.log(a) -> returns the natural logarithm of number. We have Math.log2() and Math.log10() which have base 2 and base 10 successively.
These are some of the main methods. Apart from these methods we have methods for trignometric and inverse trignometric functions and exponential function as well.
RegExp in JavaScript
RegExp stands for Regular Expressions. A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.
A regular expression seems like ->
/pattern/modifiers
Pattern is what you need to search in your text and modifiers are the operations that you want to perform on your text.
Example -:
So here you can see that the pattern is "AR" and "i" is modifier that stands for case insensitive. The output is starting index of pattern that is 14.
Similarly we can perform replace operation on text by using text.replace()
.
There are some more methods in RegExp such as exec()
and test()
methods which are used to search pattern in a string. The above ones are important.
Destructuring in JavaScript
The destructuring assignment syntax unpack object properties into variables.
We can peform destructuring on any object whether it is an array or string.
Destructuring is very important concept of JavaScript.
Let's see some of the examples-:
So this was it for this blog. I hope you have understood it well. In the later blogs we are going to see some more important concepts of JavaScript. Till then stay connected and don't forget to follow me.
Thankyou 🩵
Top comments (0)