0% found this document useful (0 votes)
99 views

Array: B. Javascript Array Directly (New Keyword)

The document discusses JavaScript arrays, events, the Document Object Model (DOM), and strings. It describes three ways to construct arrays in JavaScript using array literals, the Array constructor, or directly with new. Events like clicks are how JavaScript interacts with HTML. The DOM represents the document as a tree of objects that can be manipulated. Strings can be created with string literals or the String constructor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Array: B. Javascript Array Directly (New Keyword)

The document discusses JavaScript arrays, events, the Document Object Model (DOM), and strings. It describes three ways to construct arrays in JavaScript using array literals, the Array constructor, or directly with new. Events like clicks are how JavaScript interacts with HTML. The DOM represents the document as a tree of objects that can be manipulated. Strings can be created with string literals or the String constructor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

23.04.

2020

Array

JavaScript array is an object that represents a collection of similar type of elements. There are 3
ways to construct array in JavaScript

a) By array literal
b) By creating instance of Array directly (using new keyword)
c) By using an Array constructor (using new keyword)

a. JavaScript array literal


The syntax of creating array using array literal is given below:

var array_name=[value1,value2.....valueN];

values are contained inside [ ] and separated by , (comma).

Example : var num=[10,20,30,40,50]

Javascript code:

<script>

var num=[10,20,30,40,50]

len=num.length (.length return the length (element count) of array)

for (i=0; i<len;i++)

document.write( num[i] + “<br>”)

</script>

b. JavaScript Array directly (new keyword)

The syntax of creating array directly is given below: var arrayname=new Array();
Here, new keyword is used to create instance of array.

Javascript code:

<script>

var num= new Array()

num[0]=10

num[1]=20
num[2]=30

num[3]=40

num[5]=50

len=num.length (.length return the length (element count) of array)

for (i=0; i<len;i++)

document.write( num[i] + “<br>”)

</script>

c. JavaScript array constructor (new keyword)

you need to create instance of array by passing arguments in constructor so that we don't
have to provide value explicitly.

Javascript Code:

var num=new Array(10,20,30,40,50)

len=num.length (.length return the length (element count) of array)

for (i=0; i<len;i++)

document.write( num[i] + “<br>”)

</script>

Event
JavaScript's interaction with HTML is handled through events that occur when the user or the
browser manipulates a page.

When the page loads, it is called an event. When the user clicks a button, that click too is an event.
Other examples include events like pressing any key, closing a window, resizing a window, etc.
Developers can use these events to execute JavaScript coded responses, which cause buttons to
close windows, messages to be displayed to users, data to be validated, and virtually any other type
of response imaginable. Events are a part of the Document Object Model (DOM) Level 3 and every
HTML element contains a set of events which can trigger JavaScript Code.

onclick Event Example :

<html>

<head>
<title> Function Demo</title>

<script language="JavaScript">

function fact_num()

var num,fact=1,i

num=Number(document.getElementById("first").value)

for( i=1; i<=num; i++)

fact=fact*i

document.getElementById("ans").value=fact

</script>

</head>

<body>

Enter a Number : <input id="first"> <br>

<button onclick="fact_num()"> Answer </button> </t>

Factorial of given no is :<input id="ans">

</body>

</html>

JavaScript - Document Object Model


Every web page resides inside a browser window which can be considered as an object.

A Document object represents the HTML document that is displayed in that window. The Document
object has various properties that refer to other objects which allow access to and modification of
document content.

The way a document content is accessed and modified is called the Document Object Model, or
DOM. The Objects are organized in a hierarchy. This hierarchical structure applies to the
organization of objects in a Web document.

Window object − Top of the hierarchy. It is the outmost element of the object hierarchy.

Document object − Each HTML document that gets loaded into a window becomes a document
object. The document contains the contents of the page.

Form object − Everything enclosed in the <form>...</form> tags sets the form object.
Form control elements − The form object contains all the elements defined for that object such as
text fields, buttons, radio buttons, and checkboxes.

JavaScript String
The JavaScript string is an object that represents a sequence of characters. There are 2 ways to
create string in JavaScript:

a) By string literal
b) By string object (using new keyword)
a. By string literal
The string literal is created using double quotes. The syntax of creating string using string literal is
given below:

var string_name="string value";

simple example of creating string literal.

<script>

var str="This is string literal Demostration";

document.write(str);

</script>

b. By string object (using new keyword)


The syntax of creating string object using new keyword is given below:

var stringname=new String("string literal");

new keyword is used to create instance of string. Example of creating string in JavaScript by new
keyword.

<script>

var str=new String("hello javascript string Demostration ");

document.write(str);

</script>

You might also like