Do you know these interesting things in Javascript?

1. Why the output of the following code is so strange 🤔?

Ans: Generally, It’s expected that if the output of null > 0 and null === 0 is false then the output of null >= 0 must be false. After spending a little time on this I found that the comparison operator in javascript (>, >=, <, <= ) is trying to convert the non-numeric values into numeric ones, and here in this case javascript converts the null into 0.

You may have a question that why it’s only converted to 0 because in javascript the comparison algorithm operator is designed in such a way to convert non-numeric to numeric values. In that, it converts the boolean value(true or false) to 1 and 0, null to 0 and undefined to false. You can refer to more here.

To summarize this we can say that for the first statement null > 0 internally replace with 0 > 0 because of the javascript comparison algorithm, second statement null === 0 gives false because here we are trying to check the equality of null with 0 using equality operator (==, ===).

2. How many parameters you can pass in JSON.parse() or JSON.stringify() ?

Let’s see them one by one.

Most of the developers only used the first parameter to convert the value to a string but others parameters are also very useful.
The second parameter replacer is a function or array that is called for each value in the object. It can be used to transform the value before it is converted to a string. With it, you can specify the properties that you want to include in the output.
The third parameter is used for displaying the objects and its value is either string or a number. Generally used for displaying the indentation or linebreak. For example:

Now, suppose we want only firstName and lastName from the user Object. It can be easily done with JSON.stringify() like below.

The first parameter text is the JSON string and the second parameter is the reviewer function. This function is called on each property of the object and returns the value.

In the above example suppose we want to convert dob to Date type before it parses. It can be done with the reviewer function like below.

3. How to make an object that prevents adding new properties into objects?

It means that making an object that is not allowed to change its property value, prevents adding a new property or deleting a property from the object. Let’s see how it works.

It is used to freeze the object in a simple manner we can say that you can’t add, delete or change any properties of objects. It can also be summarised as the object is available for read-only purpose.

Note: Object.freeze() creates the shallow freeze object which means that it only works for the immediate properties of the object. Read this for more info.

If we just want the object behaves in the same manner as the object.freeze() does, but values of object properties can be writeable but the prototype of the object cannot be changed then we can use object.seal()

In simple, by using this we are allowed to change the object values but can not add or delete any object keys.

Some more interesting stuff 🥰

  1. As a JS developer, you must have used the reduce() somewhere. It’s a really good function provided by javascript and like that there is also one more function that exactly behaves like reduce() and that is reduceRight() but the difference is it works from right to left whereas reduce() will work from left to right on the array.
  2. To flat N number of the nested array, you can use Array.flat(Infinity).
  3. Nullish coalescing assignment (??=) :

The nullish coalescing assignment ( x ??= y ) operator only assigns if x is nullish (null or undefined).

4. Logical AND assignment (&&=) :

The logical AND assignment ( x &&= y ) operator only assigns if x is truthy.

5. Logical OR assignment (||=) :

The logical OR assignment ( x ||= y ) operator only assigns if x is falsy.

For more programming and JavaScript tips be sure to follow. Thank you for reading! 🥰

Happy Coding ❤️!…

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store