DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 18 of 30 of JavaScript

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

Image description

Math Methods

  1. Math.round(n) -> returns the nearest integer.
  2. Math.random() -> returns a random number between 0 (inclusive), and 1 (exclusive).
  3. Math.floor(n) -> returns the value of number rounded down to its nearest integer.
  4. Math.ceil(n) -> returns the value of number rounded up to its nearest integer.
  5. Math.trunc(n) -> returns the integer part of number.
  6. Math.min(a,b,c,d) -> return the minimum number among given numbers.
  7. Math.max(a,b,c,d) -> return the maximum number among given numbers.
  8. Math.pow(a,b) -> return a raised to power b.
  9. Math.sqrt(a) -> return the square root of a number.
  10. Math.abs(a) -> return the absolute value of given number.
  11. Math.sign(a) -> return 1 if number is positive, -1 if negative and 0 in other cases.
  12. 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 -:
Image description
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().

RegExp Modifiers
Image description

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.
Image description
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-:
Image description
Image description

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)