Home Java 9 very useful tips on JavaScript

9 very useful tips on JavaScript

by admin

Greetings Habr, I present to you a translation of the article "9 Extremely Powerful JavaScript Hacks" author Klaus
Translator’s note: this new (December 10th) post has cool JavaScript tricks which I like as a beginner, but it also has an obvious ad for a test application.But I’m obliged to translate the whole thing, so I’m hiding the advertising part in a spoiler, although it’s not useless, by the way.
Nativochka from the translated article I like to optimize.
But users don’t care about my optimized code if their site doesn’t work in Internet Explorer 11 browser.
I use Endtest to create automated tests and run them in a cross-browser cloud.
By the way, Netflix uses the same platform to test its Web applications. What’s more, it’s even listed as a required skill in some job postings
Endtest does have some cool features, such as :

  • Cross-browser grid running on Windows and MacOS computers
  • Code-free editor for automated tests
  • Support for web applications
  • Support for both native and hybrid Android and iOS apps
  • Unlimited videos for your test runs
  • Comparison of Screenshots
  • Geolocation
  • If operator
  • Cycles
  • Uploading files to your tests
  • Endtest API, for easy integration with your CI/CD system
  • Advanced statements
  • Mobile tests on real mobile devices
  • Email testing with Endtest Mailbox

You can view documents On this tool.
And now 9 very useful tips on JavaScript.

  1. Replace all

    We know that string.replace() only replaces the first occurrence. You can replace all occurrences by adding /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"
  2. Extracting unique values

    We can create a new array with only unique values by using a 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]
  3. Convert number to string

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

    var converted_number = 5 + "";console.log(converted_number);// 5console.log(typeof converted_number);// string
  4. Convert string to a number

    All we need is +operator.
    Be careful with this, as it only works with "string numbers".

    the_string = "123";console.log(+the_string);// 123the_string = "hello";console.log(+the_string);// NaN
  5. Shuffle the elements in the array

    Every day I’m shufflin’ 🙂

    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]
  6. Smooth multidimensional array

    Simple with the Spread operator.

    var entries = [1, [2, 5], [6, 7], 9];var flat_entries = [].concat(...entries);// [1, 2, 5, 6, 7, 9]
  7. Terms of the abbreviated calculation

    Let’s take this example :

    if (available) {addToCart();}

    And you can shorten it by simply using a variable along with the function :

    available addToCart()
  8. Dynamic property names

    I always thought I had to declare the object first before I could assign a dynamic property.

    const dynamic = 'flavour';var item = {name: 'Coke', [dynamic]: 'Cherry'}console.log(item);// { name: "Coke", flavour: "Cherry" }
  9. Use length to resize/clear array

    We basically overwrite the length of the array.
    If we want to change the size of the array :

    var entries = [1, 2, 3, 4, 5, 6, 7];console.log(entries.length);// 7entries.length = 4;console.log(entries.length);// 4console.log(entries);// [1, 2, 3, 4]

    If we want to clear the array :

    var entries = [1, 2, 3, 4, 5, 6, 7];console.log(entries.length);// 7entries.length = 0;console.log(entries.length);// 0console.log(entries);// []

Helpful tips on JavaScript are cool, but it pays to make sure that your web application works correctly on all browsers and on all devices. Be sure to take care of that.

You may also like