JavaScript Global Functions
// April 12th, 2011 // No Comments » // labs
In my last post, where I wrote about the Javascript problems with the float point numbers, I told that I would be writing about the JavaScript Global Functions. So here we are. This time I will talk about a few global functions that I think that could be useful and I hope that it will give you curious enough to investigate further.
Strings and Regular Expressions:
Regular Expression is not something exclusive of JavaScipt. You can use it in many languages and it provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.
A regular expression, often called a pattern, is an expression used to perform pattern-matching and “search-and-replace” functions on text. At first it seems to be very complex, but is very helpful, so is important learn how to use it. Luckily, JavaScript has several global functions that you can use when you have to work with strings. These are just some of then:
string.search(regexp)
The search() method, as its name says, searches for a match between a regular expression and a string and will returns the position of the match, or -1 if no match is found.
var theString = “search me”
document.write(theString.search("me")); //output: 7
string.substr(start,length)
The substr() method extracts the characters from a string, beginning at “start” and through the specified number of character, and returns the new sub string.
var str="Hello world!"; document.write(str.substr(3)+" "); //output: lo world! document.write(str.substr(3,4)); //output lo w
These functions are fine to make simple task, but it can get complicated without the use of regular expressions. Just to give an example, here a I leave a function to validate an e-mail just with global functions:
function validate(email){
if(email.search("@") == -1){
return false;
}else if(email.substring(0, email.search("@")).length>1){
email = email.substr(email.search("@"));
email = email.split(".");
if(email.length>1){
for(i=0;i<email.length;i++){
if(email[i] == " "){
return false;
}
}
return true;
}else{
return false;
}
}else{
return false;
}
}
And here is an alternative function, much simpler, with a regular expression:
function validate2(email) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
return true;
}else{
return false;
}
}
Math Object
The Math object allows you to perform mathematical tasks. You can use this to avoid a lot of looping and conditions. For example, if you need to get the the largest number in an array of numbers, you can do something like this with a loop:
var myNumbers = [5,635,98,17,500,950];
var max = 0;
for(var i=0;i max){
max = myNumbers[i];
}
}
alert(max);
But it is easier and better perform this:
Math.max(5,635,98,17,500,950); // returns 950
Math.max returns the number with the highest value. If no arguments are given, the result is “-Infinity”. On the other hand, if you want to get the lowest value you can perform Math.min method.
Also, maybe you have negative numbers and you want to get the absolute value of a number, to compare o do something else with it, what you need is the abs() method.
Math.abs(-7) // returns 7
