6. Random items

Generating random numbers or retrieving random items from arrays are handy and very useful methods. These methods have come up many times in my projects.

Get a random item from an array,

Generate a random number by specifying a range of minimum and maximum values:

7. Default function parameters

In JavaScript, function arguments (or arguments) are analogous to local variables of the function. When calling a function, you can either pass these values, or you can choose not to. If you do not pass the value of the parameter, it will be undefined and may cause other unnecessary conditions.

There is a simple way to pass default values to function parameters when defining parameters. The example here is that we pass the default value Hello to message as an argument to the greetings function.

8. Required function parameters

Extending the default parameter technique, we can mark parameters as required. Start by defining a function that throws an error with an error message,

The functionality is then assigned to default values for the desired parameters. Remember that default values are ignored when passing values for parameters at call time. However, if the parameter value is, the default value undefined is considered.

In the above code, name will be undefined, and you will try to set its default value, the isRequired() function. It will raise an error —

9. Comma operator

I was surprised when I realized that the comma (,) was a separate operator and had never been noticed. I’ve used it many times in code, but never realized it was so real.

In JavaScript, the comma (,) operator is used to evaluate each of its operands from left to right and return the value of the last operand.In the example above, the value of the variable is ret2. Similar to the example, the output of the following code logs the value 32 to the console.Where do we use it? Any ideas? The comma (,) operator is most commonly used to provide multiple arguments in a for loop.

Merge multiple objects

You may need to merge the two objects together and create a better information object to use. You can also use (…) The dot difference operator does the same thing. (Yes, three points!)

Consider emP and Job respectively,Using the spread operator, combine them into:There is another way to perform this merge. Use the Object. The assign (). You can also do this:Output:Note that both the spread operator and object. assign perform shallow merges. In a shallow merge, the attributes of the first object are overwritten by the same attribute values as the second object.

If you want to do a deep merge, use a method like _merge in Lodash.

11. Destructuring The technique of breaking array elements and object attributes into variables is called Destructuring. Let’s look at a couple of examples,

An array of

Here, we have a series of emojis,For deconstruction, we will use the following syntax,Letfire = emojis[0]; But with more flexibility. Did you notice that I replaced the trophy emoji with a blank area? So, what does this output?Output:A few more words about the REST operator. If you want to deconstruct an array so that you want to assign one or more items to variables and place the rest in another array, you can use something like this… The REST method does this.

Output:

object

Like arrays, we can also deconstruct objects.

By deconstructing it like this, we get the name, on both sides of a set of variables, and the rest in another object.Output:

12. Swapping variables is now very simple using the concept we just learned about destructuring.

13.isArray

There’s another useful way to determine if the input is an array.

14. Undefined vs. empty

Undefined is a declared value, but the variable is not defined.

Null itself is an empty, nonexistent value that must be explicitly assigned to the variable.

Undefined and null are not exactly equal

15. Obtain query parameters

The window.location object has a number of useful methods and properties. Using these properties and methods, we can retrieve information about protocols, hosts, ports, domains, and so on from the browser URL.

One of the features that I found really useful was,

The location. search property returns a query string from the Location URL. We can use another useful interface called URLSearchParams, along with location.search, to get the values of the query parameters.Output: js

Original link: blog.greenroots.info/my-favorite…

Author: TapasAdhikary