[Javascript] Top Javascript Tips and Tricks


In this post, We will discuss some of the tips and tricks that every javascript developer must know.

Boolean Expression

  • null, undefined, ' ' the empty string and 0 the number are all false boolean expression.
  • But be careful, '0' the string, [ ] the empty array, and {} the empty object are all true boolean expression.
so instead of using
if(x != null)
You can write the shorter format
if(x)
Similarly, if you want to check both null and empty. You can do like this:
if(x != null && x != ' ')
However, You can write short and nice
if(x)

Conditional Ternary Operator

Instead of this,
if (x) {
   foo();
} else {
   bar();
}

you can use the ternary operator,
return x ? foo( ) :bar( )

Short Circuit Operator

&& and ||, these operators are called as short circuit operators. Consider the following code.

function foo(x) {
   var win;
   if (x) {
      win = x;
   } else {
      win = window;
   }
}

You can rewrite above code as 

function foo(x) {
   var win = x || window;
}

&& is also used to reduce the lines of code, Please find the code below.

if (student) {
   if (student.name) {
      if (student.name[index]) {
         foo(student.name[index]);
      }
   }
}

You can rewrite the code as
if (student && student.name && student.name[index]) {
   foo(student.name[index]);
}

or, you can use a variable as,

var flag = student && student.name && student.name[index];
if (flag) {
   foo(student.name[index]);
}

Iteration using for loop:

For loop:

    var paragraphs = [1, 2, 3, 4, 5, 6];
    for (var i = 0; i < paragraphs.length; i++) {
       var paragraph = paragraphs[i];
       console.log(paragraph);
    }

Above code can be modified as below.

    var paragraphs = [1, 2, 3, 4, 5, 6];
    for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
       console.log(paragraph);
    }

If you enjoyed this article, please share with your developer friends.Thanks for reading.

You can follow our facebook page for latest updates.

Comments

  1. The recommended for loop is dangerous IMHO.
    If the array contains a value that evaluates to false, the loop will be aborted prematurely, e.g. with var paragraphs = [1, 0, 4]; the console will only print 1.

    ReplyDelete
    Replies
    1. Above for loop is recommended option for "Iterating over Node Lists"

      var paragraphs = document.getElementsByTagName('p');
      for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
      doSomething(paragraph);
      }


      In General, for...of statement is preferred check this post . http://www.nodesimplified.com/2017/07/javascript-list-of-dos-and-donts-in.html

      Delete

Post a Comment