4 global frequently used functions in JavaScript

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 3135 red flag

In this article we will learn four very important and frequently used global function in JavaScript.
In this article we will learn four very important global functions in JavaScript. The term global means, we can call and implement those function in any JavaScript program and from anywhere of the program. Those functions are written in library. Let’s understand them one by one.


String() function

String function is used to convert the object value to string object. The String() function is very much similar with toString() function in JavaScript. Here is sample code example of String function.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <script>
            var today = new Date();
            alert(String("Year:- " + today.getFullYear () + "Month :- " + today.getMonth()));
        </script>
    </form>
</body>
</html>

In this example we are getting full year and month from data object then we are converting them as string and concatenating with some other string. Here is output.


eval() function

evl() function is used to perform the evaluation of expression. It takes one argument and evaluate it’s result. Have a look on below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <script>
            var exp = 10 + 30 - 30 + 10;
            var result = eval(exp);
            alert("Result is:- " + result);
 
        </script>
    </form>
</body>
</html>

In this example we are evaluating one mathematical expression using eval() function. Here is output


isNaN() function

This function is used to detect whether one object is number or not. It returns true if the object is a number otherwise return false. We will implement one example with isNaN() function.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <script>
           
            var a = 10.10;
            var b = 100;
            var c = "Hello";
            alert("a is:- " + isNaN(a) + " b is:- " + isNaN(b) + " c is:- " + isNaN(c));
 
        </script>
    </form>
</body>
</html>

Here a and b are number but c is not . So that in case of c it’s showing true, means it’s not a number.


Number() function

When there is need to get corresponding number value (if possible) of certain object, we can use Number() function. Try to understand below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <script>
           
            var a = true;
            var b = 100;
            var c = "Sourav";
            alert("a is:- " + Number(a) + " b is:- " + Number(b) + " c is:- " + Number(c));
 
        </script>
    </form>
</body>
</html>

In above example we have tried to convert Boolean type using Number() function, and it’s showing “1” of corresponding “true” value. In case of string it’s showing NaN, means it’s not a number and not possible to convert.


 Conclusion

In this example we have learned four different global functions in JavaScript. Those are very much needed in real time application development. Hope you have understood them.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)