Check if your variable is an Array in Javascript.

Learn Different ways to check if a variable is array in Javascript. Using Array.isArray() Using toString() method in Object, this method convert the argument to specific type name, If we pass Object.prototype.toString.call(1) returns “[object Number]”Ā , so if we pass array then it results in “[object Array]”Ā . Using instanceof keyword arr instanceof Array; Using toString method …

Exponentiation ** operator in Javascript

The ** operator returns the result of the first variable to the power of the second variable. That is, Math.pow(a,b). Photo by Crissy Jarvis onĀ Unsplash var a = 2;var b = 5; a ** b; // 32 Math.pow(a, b); // 32 If we do like a ** b ** cĀ , then the operation is computed …

How to insert element based on index in an array.

Learn how to insert an element in specified index of the array in Javascript. The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in-place(in original array). The splice method will take three argument → startĀ , deleteCount(optional), items to add (optional)Ā . In the above program we …

Three different ways to loop through JavaScript objects

Learn how to loop through objects in Javascript. var object = { name : “Javascript Jeep”, status : “šŸ˜Ž”, icons : “šŸš—” } Using for..in With ES6, we can use Object.entries to loop through each entry of the object. The Object.entries method returns an array of [key, value]. We can also use Object.keys which will …

String methodsĀ : startsWith in Javascript.

Learn about the startsWith method of string object in Javascript. The startsWith() method returns true if the given characters are found at the beginning of the string, otherwise, false. If you want this method to be non case-sensitive then we can write a custom function We can also specify the position from which to begin …

Enabling full screen in Javascript.

Learn how to enable full screen using Javascript. We can use requestFullscreen() method on any element to make the element as full screen. requestFullscreen() method makes an asynchronous request to make the element be displayed in full-screen mode. We can check if any other element is already in fullscreen mode by using document.fullscreenElementĀ . To exit …

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) == …

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 …

Design a site like this with WordPress.com
Get started