Array: B. Javascript Array Directly (New Keyword)
Array: B. Javascript Array Directly (New Keyword)
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)
var array_name=[value1,value2.....valueN];
Javascript code:
<script>
var num=[10,20,30,40,50]
</script>
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>
num[0]=10
num[1]=20
num[2]=30
num[3]=40
num[5]=50
</script>
you need to create instance of array by passing arguments in constructor so that we don't
have to provide value explicitly.
Javascript Code:
</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.
<html>
<head>
<title> Function Demo</title>
<script language="JavaScript">
function fact_num()
var num,fact=1,i
num=Number(document.getElementById("first").value)
fact=fact*i
document.getElementById("ans").value=fact
</script>
</head>
<body>
</body>
</html>
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:
<script>
document.write(str);
</script>
new keyword is used to create instance of string. Example of creating string in JavaScript by new
keyword.
<script>
document.write(str);
</script>