Functions Used in JavaScript
Functions Used in JavaScript
isNaN()
It return true, if the object is NOT a number, it return false, if the object is a number.
Example:
document.write(isNaN(5)) // false
isNaN("55") // false, "55" is converted to the number 55
isNaN("") // false: the empty string is converted to 0 which is not NaN
isNaN(" ") // false: a string with spaces is converted to 0 which is not NaN
isNaN("six") // true
valueOf( )
It returns the value of the variable.
Example:
var X=345
document.write(X.valueOf( )) //345
var str="Hello"
result=str.valueOf( )
document.write(result) //Hello
String Operations
Length
It returns the length of a string including white space(s).
Example:
var str = "Hello javaScript";
document. write(str. length) // it returns 16.
Concat()
It joins two or more strings. The concat() method can be used instead of the plus
operator.
Example:
var text1 = "Hello"
var text2 = "World"
document.write(text1.concat( " ", text2))
Search()
It returns the starting location of passing string otherwise returns -1.
Example:
var str = "This is my first in webpage"
document.write(str. search ("first")) //returns 11
Page 1 of 4
Index0f()
This method returns the position of the first occurrence of a specified text in a string:
Example:
var str="Please visit my website!";
var pos=str.indexOf("visit");
document.write(pos) //Return 7
Replace()
Thís method replaces a specified value with another value in a string:
Example:
str="Please visit My website !";
var n = str. replace("website", "JavaScript");
document.write(n) // Please visit My JavaScript
Substring()
It extract the substring by taking the starting and ending position as argument. You can
omit ending position then it will extract the string from starting position to end of the
string.
Example:
var str ="This is my first script in JavaScript"
sub=str.substring(7, 16)
document.write(sub) // returns "my first" as substring
Mathematical operations
JavaScript Math object perform the common mathematical operations.
Math.round (x):
It returns the rounded value of passing number to its nearest integer.
Example:
document.write(Math.round (5.7)) //returns 6
document.write(Math.round (5.4)) //returns 5
Math.pow (x,y):
It returns the power of x based on y.
Example:
document.write(Math.pow(5,2)) //returns 25
document.write(Math.pow(3,4)) //returns 81
Math.sqrt(x):
It returns the square root of x.
Example:
document.write(Math.sqrt(25)) //returns 5
document.write(Math.sqrt(225)) //returns 15
Math.ceil(x):
It returns the greater or equal to integer value of x.
Example:
document.write(Math.ceil(4.1)) //returns 5
document.write(Math.ceil(5.9)) //returns 6
Page 2 of 4
Math.floor (x):
It returns the less or equal to integer value of x.
Example:
document.write(Math.floor(4.1)) //returns 4
document.write(Math.floor(5.9)) //returns 5
Implicit Conversion
In this type of conversion, JavaScript automatically converts one data type to another (to
the right type).This is known as implicit conversion. When a number is added to a string,
JavaScript Converts the number to a string before concatenation.
Example:
document.write(3 + 2) //returns 5
document.write(3 + "2") //returns 32 (because + is also concatenation operator)
document.write("3" + 2) //returns 32
document.write("3" + "2") //returns 32
document.write("10" / "2") //returns 5
document.write("3" - 2) //returns 1
document.write("3" * 4) //returns 12
parseInt()
The parseInt() function parses a string and returns an integer:
Example:
document.write(parseInt("100")) //100
parseInt("234abc") //234 cut of the remainder of the string and return the result
parseInt("2a3b4") //2
parseInt("1.5") // 1 (decimal part is truncated)
parseInt("xyz") // NaN (input can't be converted to an integer)
parseFloat()
The parseFloat() function parses a string and returns a floating point number:
Example:
document.write(parseFloat("100")) //100
document.write(parseFloat("33.33")) //33.33
String( )
String( ) method is used to convert any type to string value.
Example:
var Str=String(100+200)
document.write(Str) //300
document.write(typeof(Str)) //string
Boolean( )
Boolean( ) method is used to convert any type to Boolean type which is supported.
Example:
document.write(Boolean(0)) //returns false
document.write(Boolean(null)) //returns false
document.write(Boolean(undefined)) //returns false
document.write(Boolean(NaN)) //returns false
document.write(Boolean("")) //returns false
document.write(Boolean(false)) //returns false
Page 4 of 4