Frequently Used Simple JavaScript Functions

Introduction

In this article we will learn some frequently used simple JavaScript functions.

Let's look at the following snippet.

String () function

The String function converts an object value to a string object. The String () function is very similar to the toString() function in JavaScript.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <script>  
  8.             var today = new Date();  
  9.             alert(String("Year:- " + today.getFullYear () + "Month :- " + today.getMonth()));  
  10.  // January is 0  
  11.         </script>  
  12.     </form>  
  13. </body>  
  14. </html>  
 

eval () function

It is used to perform the evaluation of an expression. It takes one argument and evaluates its result.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <script>  
  8.             var exp = 20 + 40 - 40 + 20;  
  9.             var result = eval(exp);  
  10.             alert("Result is:- " + result);  
  11.    
  12.         </script>  
  13.     </form>  
  14. </body>  
  15. </html>  
 
 

isNaN () function

This function detects whether or not an object is a number. It returns true if the object is a number otherwise false.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <script>  
  8.               
  9.             var a = 20.20;  
  10.             var b = 200;  
  11.             var c = "Hello";  
  12.             alert("a is:- " + isNaN(a) + " b is:- " + isNaN(b) + " c is:- " + isNaN(c));  
  13.    
  14.         </script>  
  15.     </form>  
  16. </body>  
  17. </html>  
 
 
 

Number () function

In the above example we tried to convert the Boolean type using the Number() function and it's showing “1” of the corresponding “true” value. For a string it's showing NaN, in other words it's not a number and not possible to convert it.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <script>  
  8.               
  9.             var a = true;  
  10.             var b = 200;  
  11.             var c = "Sagar";  
  12.             alert("a is:- " + Number(a) + " b is:- " + Number(b) + " c is:- " + Number(c));  
  13.    
  14.         </script>  
  15.     </form>  
  16. </body>  
  17. </html>  
 
 
 

Summary

In this article we learned various frequently used global functions in JavaScript. In a future article we will learn some more basic concepts of JavaScript.
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all