Generate random numbers in JavaScript using Crypto, without Math.rand()

Learn how to get a random number without using Math.rand() The Crypto.getRandomValues() method lets you get cryptographically strong random values. Syntax: getRandomValues(typedArray) Argument: typedArray → Is an integer-based TypedArray, which can be an Int8Array or an Uint8Array or an Int16Array or a Uint16Array or an Int32Array or a Uint32Array. All elements in the array are …

Selecting , clearing and focusing on input in Javascript.

Learn how to select text, focus and clear the text in input box in Javascript. input.value = “” → this will make the input value to empty string. input.focus() → this method will set focus on the input box. input.select() → this will select the text in the input box. If you find this helpful …

Remove duplicate values of the array in Javascript.

Learn how to remove the duplicate values in the array Simple Way is using Set (which only contains unique value). var array = [1,2,3,4,1,2,3]; var unique = [… new Set(array)] ; //or var unique = Array.from (new Set(array) ); Another way is var elements = [1,2,3,4,1,2,3]; var uniqueArray = elements.filter(function(element, currentIndex) { return elements.indexOf(element) == …

Remove All children đź‘¶ of the node in Javascript.

Learn how to delete all the nodes inside a div or any node in Javascript. You can do it in three ways 1 . Set the node innerHTML as empty-string(“”). var node = document.getElementById(‘parent’); node.innerHTML = “”; This method removes all the text and nodes inside the parent node. If you need to delete only the …

Understanding weirdness of the “!” bang operator in JavaScript

There are some things we don’t know about how the ! operator works in Javascript. Basics The ! is a logical operator that will convert a value to its opposite boolean. Since JavaScript will coerce values, it will “convert” a value to its truthy/falsey form and return the opposite boolean value. When we perform the ! operation on …

Getting max of numbers in Javascript.

Learn how to get the maximum of numbers. max function in the Math object can be used to find the maximum of multiple numbers and array. Math.max(1,2); //2 Math.max(1,2,3); //3 If any one of the argument passed cannot be converted to number then it returns NaN Math.max(‘1’, 0); //1 Math.max(‘one’, 1); // NaN Math.max(1, 2, …

Flatten an array in javascript

Learn how to convert a multi-dimensional array into single dimension array in javascript We can flatten an array to single dimension array using Array.flat(depth) method. But if we have multi-dimensional array then we need to find the depth of the Array, instead of finding depth we can solve the problem in two ways we can …

Removing space in string in Javascript

Learn how to remove space in string. If you want to remove all the space in the string then , you can use replace method var name = “Javascript jeep “; name.replace(/s/g,”); // Javascriptjeep s –> spaceg –> replace globally We can also replace the space with ‘-‘ var name = “Javascript jeep “; name.replace(/s/g,’-‘); // …

Misunderstanding on parsing Boolean

Learn about how to parse boolean in Javascript. Creating a boolean value using Boolean constructor var b = new Boolean(); b is an object which have the value false in it. But the problem here is most of the beginner think as we can use directly in if , but it results to misunderstanding if(b) { …

How to convert 24 hours format to 12 hours in Javascript.

Convert the 24 hours format time to 12 hours formatted time. You can get the time by , var dt = new Date(); var hours = dt.getHours(); // gives the value in 24 hours format var minutes = dt.getMinutes() ; var finalTime = “Time – ” + hours + “:” + minutes; finalTime // final time …

Design a site like this with WordPress.com
Get started