Namespaces In JavaScript

If you have programmed in languages like C++ or Java you should be familiar with the concept of namespaces and packages. If you are only familiar with JavaScript you probably might be wondering what these things are. Lets have a look.

Alice needs to use a lengthy expression at several places in her code. To avoid repeating the expression in the code she decides to create a function for it. She names the function as calculate.

Continue reading

Posted in JavaScript | Tagged |  Comments

JavaScript Functions With Variable Number Of Arguments

You design a function called sum which returns the sum of two numbers. Now you need a function which returns the sum of three numbers. So you define another function, say sumThree, which accepts three parameters and returns their sum. Later you need a function which returns the sum of four numbers. So you define a new function sumFour. What if your requirements keep on growing? How long will you keep adding new functions?

Worried? Thankfully you don’t need to do this. JavaScript allows you to define a single function which acts differently when different number of arguments are passed to it.
Continue reading

Posted in JavaScript | Tagged , , , , |  Comments

Some Things You Should Know About JavaScript Arrays

JavaScript arrays are much more flexible than the arrays in languages like C and Java. Programmers who have used these languages and have just begun learning JavaScript assume JavaScript arrays to be similar to the ones in C and Java. These assumptions often result in inefficient code and sometimes even in incorrect code. This article discusses some of the not so well known concepts about JavaScript arrays.

Continue reading

Posted in JavaScript | Tagged , |  Comments

Introducing Delay In JavaScript Loops Using setTimeout And setInterval

JavaScript has two functions viz. setTimeout and setInterval which can be used to introduce delays in your program. But the method of using these functions is entirely different from what you might have used in other languages. People new to programming may find this style quite awkward to use.

In this article we will see how we can introduce delays between the iterations of JavaScript loops.
Continue reading

Posted in JavaScript | Tagged , , , , |  Comments