Develop a webpage using Intrinsic Java Functions

Laboratory Objective:

  1. To understand basic concepts of Forms in JavaScript.
  2. To use the given intrinsic functions with specified parameters.

Intrinsic JavaScript Functions

  • An intrinsic function (or built-in function) is a function (subroutine) available for use in a given programming language whose implementation is handled specially by the compiler.
  • “Intrinsic” is the way some authors refer to what other authors call “built-in”.
  • Those data types/objects/classes are always there regardless of what environment you’re running in.
  • JavaScript provides intrinsic (or “built-in”) objects. They are the Array, Boolean, Date, Error, Function, Global, JSON, Math, Number, Object, RegExp, and String objects.
  • As you know JavaScript is an object oriented programming language, it supports the concept of objects in the form of attributes.
  • If an object attribute consists of function, then it is a method of that object, or if an object attribute consists of values, then it is a property of that object.
  • For example,          

var status = document.readyState;

  • readyState is a property of the document object which can contain values such as “unintialized”, ”loading”, ”interactive”, ”complete” whereas,

document.write("Hello World");

  • write() is a method of the document object that writes the content “Hello World” on the web page.
  • JavaScript Built-in objects such as
  1. Number
  2. String
  3. RegExp
  4. Array
  5. Math
  6. Date
  7. Boolean
  • Each of the above objects hold several built-in functions to perform object related functionality.

isNaN()

  • isNaN() method determines whether value of a variable is a legal number or not.
  • For example

document.write(isNan(0));              // false

document.write(isNan('JavaScript'));   // true

eval()

  • eval() is used to execute Javascript source code.
  • It evaluates or executes the argument passed to it and generates output.
  • For example

eval("var number=2;number=number+2;document.write(number)");   //4

Number()

  • Number() method takes an object as an argument and converts it to the corresponding number value.
  • Return Nan (Not a Number) if the object passed cannot be converted to a number
  • For example

var obj1=new String("123");

var obj2=new Boolean("false");

var obj3=new Boolean("true");

var obj4=new Date();

var obj5=new String("9191 9999");

document.write(Number(obj1));     // 123

document.write(Number(obj2));     // 0

document.write(Number(obj3));     // 1

document.write(Number(obj4));     // 1342720050291

document.write(Number(obj5));     // NaN

String()

  • String() function converts the object argument passed to it to a string value.
  • For example

document.write(new Boolean(0)); // false

document.write(new Boolean(1)); // true

document.write(new Date());     // Tue Jan 05 2021 13:28:00 GMT+0530

parseInt()

  • parseInt() function takes string as a parameter and converts it to integer.
  • For example

document.write(parseInt("45"));         // 45

document.write(parseInt("85 days"));    // 85

document.write(parseInt("this is 9"));  // NaN

  • An optional radix parameter can also be used to specify the number system to be used to parse the string argument.
  • For example,

document.write(parseInt(“10”,16)); //16

parseFloat()

  • parseFloat() function takes a string as parameter and parses it to a floating point number.
  • For example

document.write(parseFloat("15.26"));        // 15.26

document.write(parseFloat("15 48 65"));     // 15

document.write(parseFloat("this is 29"));   // NaN

document.write(pareFloat("  54  "));        // 54

  • An intrinsic function is often used to replace the Submit button and the Reset button with your own graphical images, which are displayed on a form in place of these buttons.

<html>

<head>

<title>Using Intrinsic JavaScript Functions</title>

</head>

<body>

<FORM name="contact" action="#" method="post">

 <P>

 First Name: <INPUT type="text" name="Fname"/> <BR>

 Last Name: <INPUT type="text" name="Lname"/><BR>

 Email: <INPUT type="text" name="Email"/><BR>

 <img src="submit.jpg"              

           onclick="javascript:document.forms.contact.submit()"/>

 <img src="reset.jpg"

           onclick="javascript:document.forms.contact.reset()"/>

 </P>

</FORM>

</body>

</html>

Sample Program

Write a JavaScript function to insert a string within a string at a particular position (default is 1).

<html>

<head>

<title>Insert a string within a specific position in another string</title>

</head>

<body>

<script>

function insert(main_string, ins_string, pos) {

 if(typeof(pos) == "undefined") {

  pos = 0;

 }

 if(typeof(ins_string) == "undefined") {

  ins_string = '';

 }

 return main_string.slice(0, pos) + ins_string +

                                       main_string.slice(pos);

}

var main_string = "Welcome to JavaScript";

var ins_string = " the world of ";

var pos = 10;

var final_string = insert(main_string, ins_string, pos);

document.write("Main String: <b>" + main_string + "</b><br/>");

document.write("String to insert: <b>" + ins_string + "</b><br/>");

document.write("Position of string: <b>" + pos + "</b><br/>");

document.write("Final string: <b>" + final_string + "</b>");

</script>

</body>

</html>