Javascript Secrets : Tips and Tricks I normally use!

Javascript Secrets : Tips and Tricks I normally use! hackernoon.com5 years ago in #Dev Love68

Originally published by Srijan Agarwal on September 10th 2017 The modern web is ever-changing and JavaScript is the number one programming language in the world, the language of the web, of mobile apps, of the server side and has many other implementations. In this article, I’ll try to share some of JavaScript Secrets : Tips and Tricks I normally use, hope it help others too! 1. Use ‘var’ for creating variables. Using var for creating variables doesn’t result in global variables. Avoid global variables, they sometimes create issues, unless required. 2. Use semicolons for line termination Yes, you heard that right! Semicolons aren’t required due to the nature of JavaScript’s automatic semicolon insertion, but they’ve been been considered a best practice for years. 3. use === instead of == The == /!= operator performs an automatic type conversion, if needed. The === /!== operator will not perform any conversion! alert(” == ‘0’); //false alert(0 == ”); // true alert(0 ==’0′); // true == is not transitive. If you use === it would give false for all of these statements as expected. 4. Use logical OR (||) and logical AND (&&) for conditions The || operator first evaluates the expression on the left, if it is truthy, it returns that value. If it is falsy, it evaluates and returns the value of the right operand (the expression on the right). You can also use the logical or operator || in an assignment expression to provide a default value : var a = b || c; The a variable will get the value of c only if b is falsy (if is null, false, undefined, 0, empty string, or NaN), otherwise a will get the value of b. The && operator first evaluates the expression on the left. If it is falsy, false is returned; it does not bother to evaluate the right operand. 5. Use the ‘in’ operator to check if a key exists in an object var x = 1; var y = 3; var list = {0:0, 1:0, 2:0}; x in list; //true y in list; //false 1 in list; //true y in {3:0, 4:0, 5:0}; //true 6. Parameters for a function are not really required. Just use the function’s ‘arguments’ array-like object. function sum() { var foo = 0; for (var i = 0, len = arguments.length; i < len; i) { foo = arguments[i]; } return foo; } sum(0, 1, 2) // returns 3 7. The debugger statement This statement allows you to put breakpoints programmatically in your code just by: // ... debugger; // ... If a debugger is present or active, it will cause it to break immediately, right on that line. Otherwise, if the debugger is not present or active this statement has no observable effect. Thanks for reading. These are some of the tricks, I usually use, hope it helps you! You can follow me on Twitter, and checkout my experiences at! Comments Tags Subscribe to get your daily round-up of top tech stories!  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply