Introduction To JavaScript
Introduction To JavaScript
• Example: Output :
JavaScript Variables
Let Keyword: It allows you to declare variables that are limited to the scope of a
block statement, or expression on which it is used.
Example: Output:
JavaScript Variables
• Const Keyword: ES6 introduced the const keyword, which is used to define a
new variable in JavaScript. Generally, the var keyword is used to declare a
JavaScript variable. Const is another keyword to declare a variable when you do
not change the value of that variable for the whole program.
• The difference is just that var is for normal whose value can be changes, whereas
a variable value declared using const keyword cannot be changed.
JavaScript Variables
• Example: Output:
JavaScript Expressions
• An expression is a valid unit of code that resolves to a value. There are two types
of expressions: those that have side effects (such as assigning values) and those
that purely evaluate.
• The Expression 3+4 is an example of the second type. This expression uses the +
operator to add 3 and 4 together and produces a value 7.
• There are five primary categories of expression in JavaScript:
Array in JavaScript
• In JavaScript, the array is a single variable that is used to store different elements.
It is often used when we want to store a list of elements and access them by a
single variable. Unlike most languages where the array is a reference to the
multiple variables, in JavaScript array is a single variable that stores multiple
elements.
• JavaScript array is an object that represents a collection of similar type of
elements.
• There are 3 ways to construct array in JavaScript
• By array literal
• By creating instance of Array directly (using new keyword)
• By using an Array constructor (using new keyword)
Array in JavaScript
1) JavaScript array literal
The syntax of creating array using array literal is given below:
Example: Output:
Array in JavaScript
2) JavaScript Array directly (new keyword)
The syntax of creating array directly is given below:
Output:
•
•
Control structures in JavaScript
2. IF-Else Statement: It evaluates the content whether condition is true of false.
The syntax of JavaScript if-else statement is given below.
Flowchart:
Control Structures in JavaScript
Example: Output:
Control Structures in JavaScript
3. JavaScript If...else if statement: It evaluates the content only if expression is true
from several expressions. The signature of JavaScript if else if statement is given
below.
Control Structures in JavaScript
• Example: Output:
•
Control Structures in JavaScript
• Switch Statement: The JavaScript switch statement is used to execute one code
from multiple expressions. It is just like else if statement that we have learned in
previous page. But it is convenient than if..else..if because it can be used with
numbers, characters etc.
• The signature of JavaScript switch statement is given below.
Control Structures in JavaScript
• Example: Output:
•
Looping Statements
• The JavaScript loops are used to iterate the piece of code using for, while, do
while or for-in loops. It makes the code compact. It is mostly used in array.
• There are four types of loops in JavaScript.
• for loop
• while loop
• do-while loop
1) JavaScript For loop : The JavaScript for loop iterates the elements for the
fixed number of times. It should be used if number of iteration is known. The syntax
of for loop is given below.
Looping Statements
• Example:
Output:
Looping Statements
2. While Loop: The JavaScript while loop iterates the elements for the infinite
number of times. It should be used if number of iteration is not known. The syntax
of while loop is given below.
Example: Output:
Looping Statements
• 3. Do-While Loop: The JavaScript do while loop iterates the elements for the
infinite number of times like while loop. But, code is executed at least once
whether condition is true or false. The syntax of do while loop is given below.
Example: Output:
Document Object Model
• The document Object represents the whole html document. When html document
is loaded in the browser , it becomes a document object. It is the root element that
represents the html document. It has properties and methods. By the help of
document object, we can add dynamic content to our web page.
• The Document Object Model(DOM) is a programming interface for
HTML(Hyper Text Markup Language) and XML(Extensible Markup language)
documents. It defines the logical structure of documents and the way a document
is accessed and manipulated.
• DOM is a way to represent the webpage in a structured hierarchical way so that it
will become easier for programmers and users to glide through the document.
With DOM, we can easily access and manipulate tags, IDs, classes, Attributes, or
Elements of HTML using commands or methods provided by the Document
object. Using DOM, the JavaScript gets access to HTML as well as CSS of the
web page and can also add behavior to the HTML elements. So basically
Document Object Model is an API that represents and interacts with HTML or
XML documents.
Document Object Model
• Why DOM is required?
HTML is used to structure the web pages and JavaScript is used to add behavior to
our web pages. When an HTML file is loaded into the browser, the JavaScript can
not understand the HTML document directly . So, a corresponding document is
created(DOM). DOM is basically the representation of the same HTML document
but in a different format with the use of objects. JavaScript interprets DOM easily
i.e. javascript can not understand the tags(<h1>H</h1>) in HTML document but
can n understand object h1 in DOM. Now, JavaScript can access each of the objects
(h1, p, etc) by using different functions.
Document Object Model
• Structure of DOM: DOM can be thought of as a Tree or Forest(more than one
tree). The term structure model is sometimes used to describe the tree-like
representation of a document. Each branch of the tree ends in a node, and each
node contains objects Event listeners can be added to nodes and triggered on an
occurrence of a given event.
• One important property of DOM structure models is structural isomorphism: if
any two DOM implementations are used to create a representation of the same
document, they will create the same structure model, with precisely the same
objects and relationships.
Document Object Model
• Properties of DOM: Let’s see the properties of the document object that can be
accesses and modified by the document object.
Document Object Model
• Representation of the DOM
• Window Object: Window Object is object of the browser which is always at top of the hierarchy.
It is like an API that is used to set and access all the properties and methods of the browser. It is
automatically created by the browser.
• Document object: When an HTML document is loaded into a window, it becomes a document
object. The ‘document’ object has various properties that refer to other objects which allow access
to and modification of the content of the web page. If there is a need to access any element in an
HTML page, we always start with accessing the ‘document’ object. Document object is property
of window object.
• Form Object: It is represented by form tags.
• Link Object: It is represented by link tags.
• Anchor Object: It is represented by a href tags.
• Form Control Elements:: Form can have many control elements such as text fields, buttons,
radio buttons, checkboxes, etc.
Document Object Model