Using conditional breakpoints to log data If you wanted to log to the console a value each time a function is called, you can use conditional break points to do this. Open up your dev tools, find the function where you’d like to log data to the console ... more
We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is const timestamp = Date.now(); or const timestamp = new Date().getTime(); To get unix timestamp of ... more
We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is const dateTime = Date.now(); const timestamp = Math.floor(dateTime / 1000); or const dateTime = new ... more
As written in documentation the reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. reduce() reduce() function accepts 2 parameters (M: mandatory, O: optional): (M) ... more
Below, different ways to declare variables in JavaScript. Comments and console.log should be enough to explain what's happening here: var y, x = y = 1 //== var x; var y; x = y = 1 console.log('--> 1:', `x = ${x}, y = ${y}`) // Will print //--> ... more
The cross-browser way to check if the document has loaded in pure JavaScript is using readyState . if (document.readyState === 'complete') { // The page is fully loaded } You can detect when the document it's ready... let stateCheck = setInterval(() ... more
Below, different ways to declare variables in JavaScript. Comments and console.log should be enough to explain what's happening here: var y, x = y = 1 //== var x; var y; x = y = 1 console.log('--> 1:', `x = ${x}, y = ${y}`) // Will print //--> ... more
The built-in functions Math.max() and Math.min() find the maximum and minimum value of the arguments, respectively. Math.max(1, 2, 3, 4); // 4 Math.min(1, 2, 3, 4); // 1 These functions will not work as-is with arrays of numbers. However, there are some ... more
The cross-browser way to check if the document has loaded in pure JavaScript is using readyState . if (document.readyState === 'complete') { // The page is fully loaded } You can detect when the document it's ready... let stateCheck = setInterval(() ... more
There are two methods to find Max and Min numbers from an argument list of numbers but they does not support Arrays natively. Math.max(1, 2, 3, 4); // 4 Math.min(1, 2, 3, 4); // 1 apply() allows you to use built-ins functions to find Max Min value in ... more
JavaScript is pass-by-value, technically. It is neither pass-by-value nor pass-by-reference, going by the truest sense of these terms. To understand this passing mechanism, take a look at the following two example code snippets and the explanations. ... more
JavaScript is pass-by-value, technically. It is neither pass-by-value nor pass-by-reference, going by the truest sense of these terms. To understand this passing mechanism, take a look at the following two example code snippets and the explanations. ... more
I am sure many of you are already familiar with the ES6 Destructuring Assignment. Did you know that you can also use it in function parameters? var sayHello = function({ name, surname }) { console.log(`Hello ${name} ${surname}! How are you?`); }; sayHello({ ... more
I am sure many of you are already familiar with the ES6 Destructuring Assignment. Did you know that you can also use it in function parameters? var sayHello = function({ name, surname }) { console.log(`Hello ${name} ${surname}! How are you?`); }; sayHello({ ... more
By overriding the builtin prototypes, attackers can rewrite code to expose and change bound arguments. This can be a serious security hole that works by exploting a polyfill es5 methods. // example bind polyfill function bind(fn) { var prev = Array.prototype.slice.call(arguments, ... more
The following examples will be based on the following array: let values = [2, 56, 3, 41, 0, 4, 100, 23]; To get the average, we have to sum up numbers and then divide by the number of values. Steps are: - get the array length - sum up values - get the ... more
Let's say there is an object with properties "prop1", "prop2", "prop3". We can pass additional params to JSON.stringify to selectively write properties of the object to string like: var obj = { 'prop1': 'value1', 'prop2': ... more
It is possible to configure object properties in Javascript for example to set properties to be pseudo-private or readonly. This feature is available since ECMAScript 5.1, therefore supported by all recent browsers. To do so, you need to use the method ... more
It is possible to configure object properties in Javascript for example to set properties to be pseudo-private or readonly. This feature is available since ECMAScript 5.1, therefore supported by all recent browsers. To do so, you need to use the method ... more
These are the three known ways to merge multidimensional array into a single array. Given this array: var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; We wanna have this result: [1, 2, 3, 4, 5, 6, 7, 8, 9] Solution 1: Using concat() and apply() var myNewArray ... more
These are the three known ways to merge multidimensional array into a single array. Given this array: var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; We wanna have this result: [1, 2, 3, 4, 5, 6, 7, 8, 9] Solution 1: Using concat() and apply() var myNewArray ... more
Primitives If an Array only contains primitive values, we can deduplicate it by only using the filter and indexOf methods. var deduped = [ 1, 1, 'a', 'a' ].filter(function (el, i, arr) { return arr.indexOf(el) === i; }); console.log(deduped); // [ 1, ... more
Primitives If an Array only contains primitive values, we can deduplicate it by only using the filter and indexOf methods. var deduped = [ 1, 1, 'a', 'a' ].filter(function (el, i, arr) { return arr.indexOf(el) === i; }); console.log(deduped); // [ 1, ... more
MutationObserver is a solution to listen DOM changes and do what you want to do with elements when they changed. In following example there is some emulation of dynamic content loading with help of timers, after first "target" element creation ... more
Assigning is very common. Sometimes typing becomes time consuming for us 'Lazy programmers'. So, we can use some tricks to help us and make our code cleaner and simpler. This is the similiar use of x += 23; // x = x + 23; y -= 15; // y = y - 15; z *= ... more
Let's try out writing an asynchronous function which prints the value of the loop index every second. for (var i=0; i<5; i++) { setTimeout(function(){ console.log(i); }, 1000); } The output of the above programs turns out to be > 5 > 5 > ... more
Vamos a probar a escribir una función asíncrona que imprime el valor del índice del bucle cada segundo. for (var i=0; i<5; i++) { setTimeout(function(){ console.log(i); }, 1000); } La salida de los script antes mencionados resulta ser > 5 > ... more
Below is the line with which we can create 0...N range. Array.apply(null, {length: N}).map(Number.call, Number); Lets break down this line into parts.We know how "call" function works in javascript.So in "call" first argument will ... more
Object properties order An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method. ECMAScript Take ... more