JavaScript Tips and Tricks
JavaScript can be a frustrating language. Tame this beast with these tips and tricks.
JavaScript can be a frustrating language. Tame this beast with these tips and tricks.
If you have come to JavaScript from a background in Java or C, you will like the variation on the “for loop“. It is similar to the “for loop” in Perl, Python or PHP but with a little twist. You can use a for loop to repeatedly run a block of code or once for…
One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access any local variables you define in the outer function exactly as if you had defined them locally. You can also access any arguments you pass to a function as if…
It is possible to define multiple functions with the same name in JavaScript, and the last one you define will be the one that runs. This is unlike other languages, where you can distinguish between functions by using a different number of arguments when you call the function. Most programmers will only think about this…
JavaScript allows the use of anonymous functions with full closures. Functions are objects, and you can pass them as arguments like any other object. The scope of a function includes all the names in the scope where it was declared. As you declare functions at run time and local variables can remain after the function…
JavaScript has member functions for all objects, such as “valueOf” and “toString”. When you use literal numbers, Booleans, strings, undefined and null are different from the counterparts that you use for objects. This example shows this: There are ways that you can implicitly convert an object into a literal number, boolean or string. You can…
The JavaScript Object is very versatile and useful. It works a little like the Object base type in Java and the associative array type like %hash from Perl, dict from Python or array from PHP, and is the best of both worlds. You can also use them for String to Object mapping. You can initialize…
JavaScript is easy to write but can be difficult to debug effectively due to the limited debugging functionality it provides. One big difficulty is the lack of an effective display of debugging messages. You can overcome these limitations with the help of external debuggers. If you are developing JavaScript code for the Firefox browser, then…
Unlike most other programming languages, you can use both undefined and null values in JavaScript. This can confuse someone who comes to JavaScript from another programming language. When an object is undefined, it means that it still has to be declared or has been declared and not given a value. Null is a special value…
Assertion is a technique you can use to debug JavaScript code. You use assertion to ensure that an expression evaluates to true during script execution. This means that if any expression evaluates to false, there is possibly a bug in your code. There is no inbuilt assert function in JavaScript, but it is simple to…