I love optimization!

However, if the site doesn’t work in a user’s Internet Explorer 11 browser, they won’t care about my optimized code.

I use Endtest to create automated tests and execute them on a cross-browser cloud.

Netflix uses the same tools to test their Web apps.

You should check the documentation

Here are 9 extremely powerful JavaScript tricks.

1. Replace all

We know that string.replace() can only replace the first occurrence. You can replace all occurrences with /g at the end of the regular expression.

var example = "potato potato";
console.log(example.replace(/pot/."tom")); 
// "tomato potato"
console.log(example.replace(/pot/g."tom")); 
// "tomato tomato"
Copy the code

2. Extract unique values

We can create an array of unique values just by using the Set object and the Spread operator.

var entries = [1.2.2.3.4.5.6.6.7.7.8.4.2.1]
var unique_entries = [...new Set(entries)];
console.log(unique_entries);
// [1, 2, 3, 4, 5, 6, 7, 8]
Copy the code

3. Convert numbers to strings

We just need to use the concatenation operator with an empty set of quotes.

var converted_number = 5 + "";
console.log(converted_number);
/ / 5
console.log(typeof converted_number); 
// string
Copy the code

4. Convert the string to a number

We just need the + operator.

Note this, as it only applies to “string numbers”.

the_string = "123";
console.log(+the_string);
/ / 123

the_string = "hello";
console.log(+the_string);
// NaN
Copy the code

5. Scramble array elements

Every day I feel like I’m drinking too much

var my_list = [1.2.3.4.5.6.7.8.9];
console.log(my_list.sort(function() {
    return Math.random() - 0.5
})); 
// [4, 8, 2, 9, 1, 3, 6, 5, 7]
Copy the code

6. Flatten the multidimensional array

Very simple, use the Spread operator.

var entries = [1[2.5], [6.7].9];
varflat_entries = [].concat(... entries);// [1, 2, 5, 6, 7, 9]
Copy the code

To be precise, so-called multi-dimensional arrays work for two-digit arrays! (Translator +)

7. Short circuit conditions

Let’s take the following example:

if (available) {
    addToCart();
}
Copy the code

Shorten it by simply using variables and functions:

available && addToCart()
Copy the code

8. Dynamic property names

I always thought I had to declare an object before ASSIGNING dynamic properties. 😢

const dynamic = 'flavour';
var item = {
    name: 'Coke',
    [dynamic]: 'Cherry'
}
console.log(item); 
// { name: "Coke", flavour: "Cherry" }
Copy the code

9. Use length adjustment/empty array

We basically overwrote the length of the array.

If we want to adjust the array:

var entries = [1.2.3.4.5.6.7];  
console.log(entries.length); 
/ / 7
entries.length = 4;  
console.log(entries.length); 
/ / 4
console.log(entries); 
// [1, 2, 3, 4]
Copy the code

If you want to empty an array:

var entries = [1.2.3.4.5.6.7]; 
console.log(entries.length); 
/ / 7
entries.length = 0;   
console.log(entries.length); 
/ / 0
console.log(entries); 
/ / []
Copy the code

I think looking for JavaScript tips is really cool!

The latter

  • Original text: dev. To/razgandeanu…
  • First article: github.com/reng99/blog…
  • More: github.com/reng99/blog…