10 Mini-Tips and Tricks in JavaScript for Beginners

As I have previously stated in prior posts, JavaScript is the one of the most utilized programming languages, because a large majority of the browsers are compatible with it and make use of it. JavaScript has become very accepted, very quickly, because it is so straightforward and has a wide-range of capabilities. Many programmers used to think that JavaScript was a toy language, but the emergence of AJAX into the market manifested something completely to the contrary, because it showed JavaScript diverse capabilities and functions. Since this discovery, programmers can create web applications that give the impression of a desk top application, which is helpful because data can be changed more quickly. Here are some mini-tips and tricks to help beginners better use JavaScript. JavaScript is very wide and has so many styles, so it can get very tricky. Moreover, it has so many ways of programming, but I picked 10 tips that I think are a good starting place for beginners to understand JavaScript. If you see these scripts somewhere, here are some tips to help you through them. This is not even the tip of the iceberg with JavaScript capabilities, but it is a start! Please feel free to leave your comments, questions, extra tips, or concerns, but remember this is a post for beginners!! I love to hear from fellow developers! Enjoy!

Add an Element at the End of an Array

This tip allows you to add an element at the end of an array using the length property, because the length property is going to be one number higher than the position of the last element in the array. This way acts the same as “push” method. For example:

var myArray =[];
myArray[myArray.length]='New Element';

Resize the Length of an Array

The length property is a not read only, because you can set values to it. Moreover, you can use it to increase or decrease the size of the array. For example:

var myArray =[1,2,3];
myArray.length// 3
myArray.length=2;//Delete the last element
myArray.length=20// add 18 elements to the array; the elements have the undefined value.

Convert any Data Type to Boolean Using “!!”

This technique allows you to convert any data type, like string, number, or integer, to a Boolean using the “!!”. For example:

Organize and Improve Functions with Objects as Parameters

A very common use of objects in modern web development is to use them as parameters of a function. It is always difficult to remember the order of the parameters of a function; however, using an object is very useful because then we do not have to be concerned about the order of the parameters. Moreover, it is more organized in order to understand what we are doing. This method allows for you to organize and improve the functions with objects as parameters. For example:

function insertData(parameters){varname= parameters.name;var lastName = parameters.lastName;var phone = parameters.phone;var address = parameters.address;varstatus= parameters.status||'single'//If status is not defined as a property in the object the variable status take single as value}

To use the function now is pretty simple; we could send the data in two ways:

Functions are Data

Functions are data just like strings or numbers and we can pass them as functions parameters to create very amazing and commanding web applications. This method is very useful and is utilized by most all major frameworks. For example:

Those functions are exactly the same. The only difference is how to use them for example: the first function you can use before it is declared; however, the second one should be declared in order to work:

//Example 1
msg('Hello world');//This will workfunction msg(m){alert(m);}//Example 2
msg('Hello world');//Does not work because JavaScript cannot find the function msg because is used before is been declared.var msg =function(m){alert(m)}

Extend Native Objects

Even though some JavaScript gurus do not recommend this technique, it has been used by some frameworks. It allows you to create some helper methods, to the JavaScript API.

Contact

Tips & Tricks

Orkut gives you the option to change your profile theme but world’s most popular social networking website doesn’t provide you the ability to change theme of your profile.
In one of my my earlier post I gave you the trick to change colour of facebook using Facebook Colour changer. Now I...