1.

How to create a blink text using JavaScript?

Answer»

It is said in JavaScript that almost everything is an object. JavaScript has many NATIVE or built-in objects.

Let us see some of them:

  • The Number object represents numerical date, i.e. integers or floating-point numbers.
  • The Boolean object represents two values, i.e. "true" or "FALSE".
  • The String object lets you work with a series of characters.
  • The Array object stores multiple values in a single variable.
  • The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date().
  • The math object provides you properties and methods for mathematical constants and functions.

Example: Create an object

Let us now see how to create an object in JavaScript. To create an object in JavaScript, use “new” as shown below. It is followed by the constructor method Object():

var department = new Object();

Let us see an example to create an object using Object() constructor:

&LT;html>    <head>       <title>Objects</title>       <script>          var department = new Object();          department.name = "FINANCE";          department.id  = 005;          department.location = "North";       </script>    </head>    <body>       <h2>Department Details</h2>       <script>          document.write("Department Name = " +  department.name + "<br>");          document.write("Department Id = " + department.id + "<br>");           document.write("Department Location = " + department.location);       </script>    </body> </html>

The output:

Above, firstly we have created an object: 

var department = new Object();  

After that properties are assigned:

department.name = "Finance"; department.id  = 005; department.location = "North";

Example: Create a Boolean Object

The Boolean object represents two values, i.e. "true" or "false". Let us now see an example wherein we will create a Boolean object and return the VALUE. We are using Boolean valueOf() method:

<!DOCTYPE html> <html> <body> <script>    var val = new Boolean(true);    document.write( "Value = " + val.valueOf() ); </script> </body> </html>

The output: 

Value = true

Here is another example wherein we are returning the string representation of a Boolean value. We are creating a Boolean object first:

<!DOCTYPE html> <html> <body> <script>    var val = new Boolean(false);    document.write( "String = " + val.toString() ); </script> </body> </html>

The output:

String = false

Example: Create a Number Object

The Number object represents numerical date, i.e. integers or floating-point numbers.

Let us now see how to create a Number object:

var val = new Number(25);

Let us now create an object with floating-point value:

var val = new Number(35.90)

An example here is to get the value of the specified Number object. We are creating a Number object:

<html>    <head>       <title>JavaScript Number Object</title>    </head>    <body>       <script>          var val = new Number(35.70);          document.write("Value = " + val.valueOf());       </script>    </body> </html>

The output:

Value = 35.7

Let us now create another Number object and represent it in String:

<html>    <head>       <title>JavaScript Number Object</title>    </head>    <body>       <script>          var val = new Number(90);          document.write("String: " + val.toString());       </script>    </body> </html>

The output: 

String: 90


Discussion

No Comment Found