JavaScript utilities #1

Well, well, well… Here is the second post, it took me to write it more time that I though but here is it.

Today I’ll show you a few util functions you can use in and save a lot of time.

.trim()
Trim is a function to delete blank spaces at the start or at the end of a string. It will include spaces, tabs, no-break space, etc.

var foo = “    Jhon Doe    “;
console.log(foo.trim());
//Jhon Doe

isNan() - (Not a Number)

Verify if is a number. Return true if it’s not.

var number = 5;
var foo = “Not a number”;
number.NaN() // false
foo.Nan() // true

let

You can use it to define variables creating a specific scope, it works similar to var, but this last one define the variable inside the scope it was create it and this one could be accessed by inner scopes. Now, with let you only can access the variable inside the scope where it was create it and it will no could be accessed by inner scopes.

if(x == y){
let foo = “Something here”;
console.log(foo);
  if(z = x){
console.log(foo);
}
}

In the above code, foo is define inside the first if and it will be printed immediately but will not be printed in the inner if.

.split()

Split strings into substrings and return an array with those strings. You can define which is the parameter to split the string.

var foo = “Lorem ipsum dolor sit amet”
//The string would be split by white spaces
console.log(foo.split(‘ ‘));
//print ["Lorem", "ipsum", "dolor", "sit", "amet"]

Now, you can define where where to stop.

var myString = “Lorem ipsum dolor sit amet”
var splitted = myString.split(‘ ‘, 3);
console.log(splitted);
//print ["Lorem", "ipsum", "dolor"]

Ok, this would be everything for now. In the next few days I will post another util functions.

Hope you enjoyed , happy coding. :)