Implement copy-on-click using JavaScript

We have seen many websites have the option to copy a block of text by clicking a button. Let’s learn how to build this using vanilla JavaScript. Steps to copy text in javascript 1. Create a textarea element 2. Get the text from the div using div.innerText 3. Set the value of textarea with the …

Find the number of days between two dates in Javascript

Learn to find the difference between two dates in javascript. Let’s take an example of what we are going to do. We have a function that takes two dates as the input, and it returns the number of days. If we pass May 22, 2019 and May 28, 2019, then it returns 7. The solution …

Different ways to create Date in JavaScript.

Learn about Date object in JavaScript To get the current time and date create a Date object without any arguments. var now = new Date(); log(now) // Tue Mar 12 2019 21:14:43 GMT+0530 (India Standard Time) There are many ways of creating a Date object Passing milliseconds as arguments If there is single argument and …

Javascript Equivalent method for jQuery addClass method.

The jQuery addClass method will add the class provided to the element or elements. Consider that we have an “div” element stored in the variable “divElement” . <div id = “divElement”> </div> <script> let divElement = document.getElementById(‘divElement’)</script> now we can add class to the “divElement” in JavaScript by function addClass(element, className) { divElement.classList += ” className”; …

Understand filter in javascript.

Javascript filter method. filter() is used to filter the array and creates a new array with the elements which satisfy the condition provided as predicate function . filter method takes a predicate( predicate is commonly understood to be a Boolean-valued function P: X→ {true, false}) as argument , to test each element of the array satisfy condition …

Get keys of the object in javascript

Object.keys() method can be used to get the keys of the object. We get the keys in same order as we get in the normal for..in loop It returns an array filled with the keys of the object. // array var num = [1 , 2, 3];console.log(Object.keys(num)); // [‘0’, ‘1’, ‘2’] // objectvar obj = …

The correct way to delete an object and its referring objects in javascript .

Let’s create an object let obj1 = { name : “Stark”, age : 23} Create another variable with the value referring to the obj1 let obj2 = obj1; Now we know that whatever operation we do on obj1 alters the value in obj2 and vice versa. obj1.job = “superhero”; obj1 // {name: “Stark”, age: 23, …

Design a site like this with WordPress.com
Get started