Posts

Showing posts with the label tip

JavaScript variables cheatsheet

Here are some different ways by which you can create variables in JavaScript var a; // variable var b = "init"; // string var c = "Hi" + " " + "Joe"; // = "Hi Joe" var d = 1 + 2 + "3"; // = "33" var e = [2,3,5,8]; // array var f = false; // boolean var g = /()/; // RegEx var h = function(){}; // function object const PI = 3.14; // constant var a = 1, b = 2, c = a + b; // one line let z = 'zzz'; // block scope local variable

Delete Object property in Javascript

Image
You can remove properties from an object using the delete operator. The delete operator removed both the value and the property from the object.   Here is an example

Remove falsy values from a JavaScript array using filter method

Here are examples of Falsy values (Falsy values returns false on evaluation) in JavaScript: undefined , null , NaN , 0 , "",  false We can use the JavaScript filter method to remove such values from our array var valueArray = ["Nishant", "IT", false, "India", 0, NaN, "JavaScript", undefined, null]; console . log ( valueArray . filter ( Boolean ) ); // returns ["Nishant", "IT", "India", "JavaScript"]

Truncate JavaScript array using length

Here are few examples to truncate JavaScript array using length property var chars = ["a", "w", "r", "t", "y", "y"]; console.log(chars); // ["a", "w", "r", "t", "y", "y"]; chars.length = 2; console.log(chars); // ["a", "w"]; chars.length = 0; console.log(chars); // [];

Copy JS path from browser devtools

Image
With DevTools (Chrome 72) you can copy JS path from your browser