JavaScript

Tips and tricks I learned throughout the years.

JavaScript is different. Experienced users that come from other languages have a hard time learning it, because it looks similar but operates differently. Here I will share all the insights I learned in the past few years. You will find it quite useful whether you programmed for years, or are just getting started.

JavaScript tips — Page 6

String trimming

In modern browsers, JavaScript has a .trim() function on the String object. What it does is simple, it removes the extra white space before and after the string. var text = " I have space before …

Read

Render unsupported HTML5 tags in Internet Explorers

Old versions of Internet explorer do not support new html5 tags. In HTML5 the `` tag for example allows you to embed a video on your page directly into your page, without having to resort to flash. HT…

Read

Use the triple equal for conditions to avoid type coercion

JavaScript comparison symbol does not work in the same fashion as other languages. The double equal symbol == performs something called type coercion before doing the comparison. What it means is, it …

Read

Every statement must end with a semicolon;

The JavaScript interpreter is nice enough to add semicolons for us if we forget them. For most places this works, but then you get a bug that is impossible to fix, and the debugger doesn't give you th…

Read

Combine all variable declarations

It is common to see code where all variables are declared separately in JavaScript. When in a scope, or a function, we have tendency to declare variables in multiple places thinking they are isolated …

Read

Checking if variable is an Array

Since the last time I tackled this issue, there has been some improvements to newer versions of JavaScript. The Array object now has the isArray() method. To check if an object is an array, all you ha…

Read

SetTimeout and SetInterval use eval therefore are evil

Eval is evil. Ok, it's not like it goes out at night and kill kittens but it is evil nevertheless. Let's start from the least evil to the kitten killer. Code passed through the eval compiler is run…

Read

For loop does not create a new scope

Unlike in other languages, the content of a for loop is not in a new scope. Let me show you. var elements = document.getElementsByTagName("*"); for (var i = 0; i < 10; i++){ // inside the …

Read

Photography