0% found this document useful (0 votes)
3K views

Functions Used in JavaScript

The document discusses JavaScript functions and built-in functions like isNaN() and valueOf(). It also covers string operations such as length, concat(), search() and substring. The document then discusses mathematical operations using the Math object and methods like round(), pow(), sqrt(), ceil() and floor(). Finally, it discusses data type conversion in JavaScript, including implicit and explicit conversion, and methods to convert between types like Number(), parseInt(), parseFloat() and String().

Uploaded by

salwa siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Functions Used in JavaScript

The document discusses JavaScript functions and built-in functions like isNaN() and valueOf(). It also covers string operations such as length, concat(), search() and substring. The document then discusses mathematical operations using the Math object and methods like round(), pow(), sqrt(), ceil() and floor(). Finally, it discusses data type conversion in JavaScript, including implicit and explicit conversion, and methods to convert between types like Number(), parseInt(), parseFloat() and String().

Uploaded by

salwa siddiqui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task. JavaScript


provides a number of built-in functions.

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

Data Type Conversion


The process of converting one data type to another data type is called type conversion.
There are two types of type conversion in JavaScript.
1. Explicit Conversion
2. Implicit Conversion

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

document.write (1 + true) //Return 2


document.write (1 + false) //Return 1 (because false is converted to 0)

document.write(3 + null) // Return 3 (because null is converted to 0)


document.write("3" + null) // Return 3null
document.write(true + null) // Return 1
document.write(false + null) // Return 0

document.write (1 + undefined); // Return NaN


document.write (true + undefined); // Return NaN

Explicit Type Conversion:


You can also convert one data type to another as per your needs. The type conversion
that you do manually is known as explicit type conversion. This type of conversions are
done using built- in methods.

Converting Values to Numbers


Number():
This method is used to convert any type to number type.
Example:
document.write(Number("10.5")) //returns 10.5
document.write(Number(true)) //returns 1
document.write(Number(false)) //returns 0
document.write(Number(null)) //returns 0
Page 3 of 4
document.write(Number(" ")) //returns 0
document.write(Number("")) //returns 0
document.write(Number("hello")) //returns NaN

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

document.write(Boolean("0")) //returns true


document.write(Boolean(1)) //returns true
document.write(Boolean(true)) //returns true
document.write(Boolean("hello")) //returns true
document.write(Boolean(" ")) //returns true

Page 4 of 4

You might also like