JavaScriptIn this article we take a look at two functions in JavaScript that helps in processing data, variables and interaction in the application. The "undefined" in JavaScript is not just dummy information for the application to ignore; it has a specific role which requires proper implementation. Return false on the other hand has been used extensively but its unique role was not fully appreciated.
The Role of Undefined and Proper Usage
The variable "undefined" for Ajax and JavaScript applications is not a rare occurrence. Developers will always use this variable in order to create a shell for the application. This variable simply doesn't contain anything but in no way will destroy the application.
But developers don't just ignore the variable and code it without additional operators. For example, developers can't just code the variable this way:
if(variable = = 'undefined')
In this setting the undefined is not clearly defined for the application. With this coding, a buggy application could be created because the JavaScript engine might read the variable as object, string or even data. It might be possible that the application will not work at all.
Because of this problem, it is important to use the operator "typeof" before "variable" to indicate the true role of "undefined". This is what happens when the operator is added:
if(typeof(variable = = 'undefined')
The code is very easy to add and will most likely deal with common problems regarding the 'undefined' variable.
Knowing More about Return False
Return False is probably one of the basic codes added in JavaScript and Ajax based applications. This is often implemented in "onclick" functions. The "return false" function is added after the function asked by onclick. The code will usually go this way:
onclick="function(),return false"
The onclick function obviously denotes the action required from users. As soon as the expected action happens, the function is implemented. Return false doesn't have anything to do with the application. There should be something that the function does.
To know the effect of the return false, try to remove this code in the onclick function. What would happen is that the webpage will perform the action needed but the user will jump to the top of the webpage. With return false, the browser doesn't jump to the top of the page.
The reason for this jump without the return false function is the inherent functionality of browsers with onclick and other actions in the applications. The browser presupposes that the webpage will refresh because new information will be loaded in the browser. But since Ajax and JavaScript based applications doesn't refresh the browser, it just jumps to the top of the page without doing anything. The return false is added to prevent the browser to jump on top of the page. It's basically a function to prevent an annoying online application.
Undefined and Return False are simple functions in Ajax and JavaScript based applications but can drastically improve the experience of users. These are common functions and should be checked to ensure proper implementation.
| Comments |
|---|