browse by category or date

Today Medium sent me its weekly summary. One of the link that caught my eye was “5 JavaScript Tricks That Are Good To Know“. I decided to check it out hoping to learn something. After all, I might not know what I don’t know, you know.

1. Array operation using every and some

This one I know 😀

var array1 = [1,2,3,4,5];
var array2 = [-1,0,4,10,11];

var isPositive = (num) => {
   return num > 0;
}
console.log(array1.every(isPositive)); // true
console.log(array1.some(isPositive)); // true

console.log(array2.every(isPositive)); // false
console.log(array2.some(isPositive)); // true

2. Setting default value of a parameter

This one I also know.

function DoSomething(option) {
   // if option.firstOption is null / undefined, option_one value will be 'Default First Option'
   var option_one = option.firstOption || 'Default First Option';
}

3. Casting values in Arrays

This one is using map, which I am aware of its usage. But what I don’t know that is now shown below:

var array = ['1', '2', '3'];
var number_array = array.map(Number); // number_array is now [1, 2, 3]

4. Object destructuring

This one I don’t know. But somehow reminds me of C#’s Tuples

var obj = {
   param1: "Value 1",
   param2: "Value 2",
   param3: 3
};

var { param1, param2, param3 } = obj;

console.log(param1); // "Value 1"
console.log(param2); // "Value 2"
console.log(param3); // 3

5. Debug performance measure using console.time

This one I don’t know. You apparently can measure how long a method runs:

console.time('loop');
 
for (let i = 0; i < 10000; i++) {   
    // Do stuff here 
}  

console.timeEnd('loop');

Haha.. I exactly know 2.5 of the tricks here. Check out the link above for more detailed explanation.

GD Star Rating
a WordPress rating system
[READ] 5 JavaScript Tricks That Are Good To Know, 3.0 out of 5 based on 1 rating

About Hardono

Hi, I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

No Comment

Add Your Comment