Follow the two alternatives below to compare objects in JavaScript

Most of the time, you use the == and === operators in JavaScript to compare data types like int and strings. But you can't compare objects with == and ===.

JSON.stringify()

One way to fix this is to turn both objects into strings and then use the equality operators.
First, we use JSON.stringify() to turn the objects into their stringified versions. Then, we use ===: to compare them.

Even though the properties are the same, the above method will return false if the order of the properties is different

Using lodash

Instead, we can use a JavaScript library called lodash to solve this problem.

The _.isEqual(value1, value2) method in the JavaScript library lodash does a deep comparison between two values to see if they are the same.

        
    

Use %c directive in your console.log message and add CSS style rules in your messages.

Using the %c directive, you can apply CSS styling in your console.log messages. The styling will be applied in the text after the directive. You can define %c many times in your message. Every time you add %c you have to define the same number of CSS style blocks as parameters.

console.log('This message has one %cCSS rule', ‘color: red’);

console.log('This message has two %cCSS %crules', 'color: red', 'font-size: 20px');

        
    

Use JavaScript to scroll to the bottom of the page automatically.

In order to automatically scroll to the bottom of the page, you can use the native function scrollTo method of the window object, which accepts either two parameters describing the x and y coordinates or one options parameter.

options: {  
 top: number,
 left: number,
 behavior: ‘smooth’ | ‘auto’
})

        
    

The Clipboard API allows developers to respond to clipboard instructions (cut, copy, and paste) and asynchronously read from or write to the system clipboard.

Copy/paste is one of the most often used functionalities in modern computing, and it refers to the process of copying or transferring text or images from one section of a computer-based application to another. Recently, it has become normal practice to copy some material programmatically to a user's clipboard, so that they don't have to do it using their traditional Ctrl + C.

JavaScript: copy to the clipboard

The entire procedure can be reduced to a button (or other element) that activates the code! You could also do this on the page load, but it is generally prudent to be cautious when automatically interacting with the user and executing actions that affect their local machine (such as copying anything to the clipboard).

        
    

How to open a link/url in new window or tab

The open() method is part of the global variable Window. Window is representing where the script is running and exposes the JavaScript code.

With the open() method you can open a new window or tab the specified url.

Accepts three parameters, all optional.

open()
open(url)
open(url, target)
open(url, target, windowFeatures)

url
The url is what the new window will try to open

target
The target specifies the context of the newly created window or tab. If the passed target doesn't identify an existing context, a new context will be created. You can also use the special keywords (_self, _blank, _parent,  _top) .

windowFeatures
String containing comma-separated list of window features. ex. height=200

 


 

        
    

array.sort()

Using sort() method you can define the sorting in an array. Returns the reference to the same array, the sort() method was applied but this time sorted. The default sort order is ascending, but a custom ordering rules can be applied using a compare function.

The compare function specifies a function that defines how the array will be sorted. It accepts to arguments (a, b) which are the first and second elements for comparison.