Professional Documents
Culture Documents
Chapter1
Origin and evolution of html
HTML
HTML stands for Hyper Text Markup Language
HTML is the standard markup language for creating Web pages
HTML describes the structure of a Web page
HTML consists of a series of elements
HTML elements tell the browser how to display the content
</body>
</html>
Comment in html
The comment tag is used to insert comments in the source code. Comments are not displayed in the
browsers.
You can use comments to explain your code, which can help you when you edit the source code at a
later date. This is especially useful if you have a lot of code.
<p>This is a paragraph.</p>
HTML TAG
HTML tags are like keywords which defines that how web browser will format and display the content.
With the help of tags, a web browser can distinguish between an HTML content and a simple content.
HTML tags contain three main parts: opening tag, content and closing tag.
HTML tag which is usually written as <html>…. </html> or <HTML>…. </HTML> is the only tag
that is a must for writing HTML pages. HTML tag has both an opening <html> and a closing
tag </html>. When a web browser reads an HTML document, browser reads it from top to bottom and left
to right.
HEAD TAG
The <head> tag in HTML is used to define the head portion of the document which contains
information related to the document. The information contained by the <head> element is not
displayed by the browser on the web-page.
The <head> tag contains other head elements such as <title>, <meta>, <link>, <style> <link>
etc.
In HTML 4.01 the <head> element was mandatory but in HTML5, the <head> element can be
omitted.
TITLE TAG
The title tag is an HTML code tag that allows you to give a web page a title.
The <title> element resides in the <head> tag and defines the name of the web page.
BODY TAG
HTML <body> tag defines the main content of an HTML document which displays on the browser.
It can contain text content, paragraphs, headings, images, tables, links, videos, etc.
The <body> must be the second element after the <head> tag or it should be placed between </head>
and </html> tags. This tag is required for every HTML document and should only use once in the
whole HTML document.
<p>This is a paragraph.</p>
2. Line breaks
Normally we write the paragraphs in several lines and if we want break point and start at next line, then we
should use the break tag.
Break tag doesn't have any content so starting the tag and then closing that one is not necessary in this case. Just
insert the tag <br/>.
Example:
<p>If you want to break a line <br> in a paragraph, <br> use the BR element in <br> your HTM
L document. </p>
Output:
If you want to break a line
in a paragraph,
use the BR element in
your HTML document.
3. Heading tag
A HTML heading or HTML h tag can be defined as a title or a subtitle which you want to display
on the webpage. When you place the text within the heading tags <h1>.........</h1>, it is displayed
on the browser in the bold format and size of the text depends on the number of heading.
There are six different HTML headings which are defined with the <h1> to <h6> tags, from highest
level h1 (main heading) to the least level h6 (least important heading).
<body>
</body>
Output:
Heading no. 1
Heading no. 2
Heading no. 3
Heading no. 4
Heading no. 5
Heading no. 6
4. Centering Content
You can use <center> tag to put any content in the center of the page or any table cell.
<center>
<p>This text is in the center.</p>
</center>
5. Horizontal Lines
Horizontal lines are used to visually break-up sections of a document. The <hr> tag creates a line
from the current position in the document to the right margin and breaks the line accordingly.
<hr />
<p>This is paragraph two and should be at bottom</p>
Again <hr /> tag is an example of the empty element, where you do not need opening and closing tags, as
there is nothing to go in between them.
HTML – Formatting
HTML Formatting is a process of formatting text for better look and feel. HTML provides us ability to
format text without using CSS. There are many formatting tags in HTML. These tags are used to make text
bold, italicized, or underlined.
1. Bold Text
In HTML, <b> tag and <strong> tag display the text in bold front.
The HTML <b> element is a physical tag which display text in bold font, without any logical importance.
If you write anything within <b>............</b> element, is shown in bold letters.
The HTML <strong> element defines text with strong importance. The content inside is typically
displayed in bold.
2. HTML <i> and <em> Tag
The HTML <i> tag displays the text in italics. It begins with <i> tag and ends with </i> tag.
<i>This text is italic</i>
The HTML <em> element defines emphasized text. The content inside is typically displayed in
italic.
<em>This text is emphasized</em>
5. Strike Text
Anything written within <strike>.......................</strike> element is displayed with strikethrough. It is a
thin line which cross the statement.
Example:
Output:
Write Your First Paragraph with strikethrough.
6. Superscript Text
The HTML <sup> element defines superscript text. Superscript text appears half a character
above the normal line, and is sometimes rendered in a smaller font.
Example: <p>x<sup>2</sup>+y<sup>3</sup> </p>
Output: x2+y3
7. Subscript Text
The HTML <sub> element defines subscript text. Subscript text appears half a character below
the normal line, and is sometimes rendered in a smaller font. Subscript text can be used for
chemical formulas, like H2O:
Example: <p>H<sub>2</sub> O</p>
8. Deleted Text
The HTML <del> element defines text that has been deleted from a document. Browsers will
usually strike a line through deleted text.
Example: <p>Hello <del>Delete your first paragraph.</del></p>
Output: Hello Delete your first paragraph
9. Inserted Text
The HTML <ins> element defines a text that has been inserted into a document. Browsers will
usually underline inserted text
Example: p>My favorite color is <del>blue</del> <ins>red</ins>.</p>
Output: My favorite color is blue red
New Semantic/Structural element of html5
Many web sites contain HTML code like: <div id="nav"> <div class="header"> <div
id="footer"> to indicate navigation, header, and footer.
In HTML there are some semantic elements that can be used to define different parts of a web
page
When deciding between an article or section, consider whether the content would still make sense
if taken out of its current page. This indicates and article.
A section on the other hand, is only a part of a greater whole and does not stand on its own.
Chapters
Introduction
News items
Contact information
<article>
<h2>Mozilla Firefox</h2>
<p>Mozilla Firefox is an open-source web browser developed by Mozilla
</article>
Aside tag
The <aside> element defines some content aside from the content it is placed in (like a sidebar).
pull quotes
background information
supplementary links
Nav tag
The <nav> element defines a set of navigation links. There can be multiple <nav> elements on a
page. However, this element should be reserved for primary navigational areas rather than simply
lists of links.
<nav>
<ul>
<li><a href="mac.html" title="Mac computers">Mac</a></li>
<li><a href="ipad.html" title="Learn about iPads">iPad</a></li>
<li><a href="iphone.html" title="Learn about iPhones">iPhone</a></li>
<li><a href="support.html" title="Get support">Support</a></li>
</ul>
</nav>
Header tag
he <header> element represents a container for introductory content or a set of navigational links.
Note: You can have several <header> elements in one HTML document.
However, <header> cannot be placed within a <footer>, <address> or another <header> element.
Example:
<article>
<header>
<h1>What Does WWF Do?</h1>
<p>WWF's mission:</p>
</header>
<p>WWF's mission is to stop the degradation of our planet's natural environment,
and build a future in which humans live in harmony with nature.</p>
</article>
Footer tag
copyright information
contact information
sitemap
back to top links
related documents
Example:
<footer>
<p>Author: Hege Refsnes</p>
<p><a href="mailto:hege@example.com">hege@example.com</a></p>
</footer>
The <figure> tag specifies self-contained content, like illustrations, diagrams, photos, code
listings, etc.
The <figcaption> tag defines a caption for a <figure> element. The <figcaption> element can be
placed as the first or as the last child of a <figure> element.
The <img> element defines the actual image/illustration.
Example:
<figure>
<img src="pic_trulli.jpg" alt="Trulli">
<figcaption>Fig1. - Trulli, Puglia, Italy.</figcaption>
</figure>
Example:
It is a necessary attribute that describes the source or path of the image. It instructs the browser
where to look for the image on the server.
2) alt
The alt attribute defines an alternate text for the image, if it can't be displayed. The value of the alt
attribute describe the image in words.
3) width
It is an optional attribute which is used to specify the width to display the image. It is not
recommended now. You should apply CSS in place of width attribute.
4) height
It is used to specify the height of the image
Example:
We can also link an image with other page or we can use an image as a link. To do this, put <img>
tag inside the <a> tag.
<a href="https://www.javatpoint.com/what-is-
robotics"><img src="robot.jpg" height="100" width="100"></a>
List tag
HTML Lists are used to specify lists of information. All lists may contain one or more list elements.
There are three different types of HTML lists:
An unordered list is a collection of related items that have no special order or sequence. This list is
created by using HTML <ul> tag. Each item in the list is marked with a bullet.
Example
<ul>
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ul>
Beetroot
Ginger
Potato
Radish
you can use type attribute for <ul> tag to specify the type of bullet you like. By default, it is a
disc. Following are the possible options :
In the ordered HTML lists, all the list items are marked with numbers by default. It is known as
numbered list also. The ordered list starts with <ol> tag and the list items start with <li> tag.
<ol>
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
This will produce the following result
1. Beetroot
2. Ginger
3. Potato
4. Radish
Example:
<ol type = "I">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
This will produce the following result:
I. Beetroot
II. Ginger
III. Potato
IV. Radish
The definition list is the ideal way to present a glossary, list of terms, or other name/value list.
Definition List makes use of following three tags.
<dl>
<dt><b>HTML</b></dt>
<dd>This stands for Hyper Text Markup Language</dd>
<dt><b>HTTP</b></dt>
<dd>This stands for Hyper Text Transfer Protocol</dd>
</dl>
Table tag
HTML table tag is used to display data in tabular form (row * column). There can be many
columns in a row.
We can create a table to display data in tabular form, using <table> element, with the help of <tr> ,
<td>, and <th> elements.
In Each table, table row is defined by <tr> tag, table header is defined by <th>, and table data is
defined by <td> tags.
HTML tables are used to manage the layout of the page e.g. header section, navigation bar, body
content, footer section etc. But it is recommended to use div tag over table to manage the layout of
the page .
Tag Description
<body>
<table border = "1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
Here, the border is an attribute of <table> tag and it is used to put a border across all the cells.
Table Heading
Table heading can be defined using <th> tag. This tag will be put to replace <td> tag, which is used
to represent actual data cell.
<table border = "1">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
Tables Backgrounds
You can set table background using one of the following two ways −
bgcolor attribute − You can set background color for whole table or just for one cell.
background attribute − You can set background image for whole table or just for one cell.
Form tag
An HTML form is used to collect user input. The user input is most often sent to a server for
processing.
The <form> Element
The HTML <form> element is used to create an HTML form for user input:
<form>
.
form elements
.
</form>
The <form> element is a container for different types of input elements, such as: text fields,
checkboxes, radio buttons, submit buttons, etc.
The HTML <form> element can contain one or more of the following form elements:
1. <input>
2. <label>
3. <select>
4. <textarea>
5. <button>
6. <fieldset>
7. <legend>
8. <datalist>
9. <output>
10. <option>
11. <optgroup>
The <input> Element One of the most used form element is the <input> element.
The <input> element can be displayed in several ways, depending on the type attribute.
Here are the different input types you can use in HTML:
<input type="button">
<input type="checkbox">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="image">
<input type="month">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
First name:
Last name:
Placeholder Attribute:
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" placeholder=”Enter your first name”>
Example:
<form>
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd">
</form>
Username:
Password:
Last name:
Input Type Radio
Radio buttons let a user select ONLY ONE of a limited number of choices:
If you use one name for all the radio button only one radio button can be selected.
<form>
<label> Gender: </label> <br>
<input type="radio" id="gender" name="gender" value="male"/> Male <br>
<input type="radio" id="gender" name="gender" value="female"/> Female <br/>
</form>
Input Type Checkbox
<input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example:
<form><input type="checkbox" id="vehicle1"name="vehicle1"value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
I have a bike
I have a car
I have a boat
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
Input Type Color
The <input type="color"> is used for input fields that should contain a color.
Depending on browser support, a color picker can show up in the input field.
Example:
<form>
<label for="favcolor">Select your favorite color:</label>
<input type="color" id="favcolor" name="favcolor">
</form>
Input Type Date
The <input type="date"> is used for input fields that should contain a date.
Depending on browser support, a date picker can show up in the input field.
Example:
<form>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
</form>
We can also use the min and max attributes to add restrictions to dates:
Input Type Email
The <input type="email"> is used for input fields that should contain an e-mail address.
Depending on browser support, the e-mail address can be automatically validated when submitted.
Some smartphones recognize the email type, and add ".com" to the keyboard to match email
input.
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
</form>
The <input type="file"> defines a file-select field and a "Browse" button for file uploads.
<form>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
</form>
Input Type Range
The <input type="range"> defines a control for entering a number whose exact value is not
important (like a slider control). Default range is 0 to 100. However, you can set restrictions on
what numbers are accepted with the min, max, and step attributes:
<form>
<label for="vol">Volume (between 0 and 50):</label>
<input type="range" id="vol" name="vol" min="0" max="50">
</form>
Input Type Url
The <input type="url"> is used for input fields that should contain a URL address.
Depending on browser support, the url field can be automatically validated when submitted.
Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.
<form>
<label for="homepage">Add your homepage:</label>
<input type="url" id="homepage" name="homepage">
</form>
The <label> element defines a label for several form elements.The <label> element is useful for
screen-reader users, because the screen-reader will read out loud the label when the user focus on
the input element.
The <label> element also help users who have difficulty clicking on very small regions (such as
radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it
toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of the <input> element to
bind them together.
The <option> tags inside the <select> element define the available options in the drop-down list
The <select> element is used to create a drop-down list.
Output:
The <fieldset> element is used to group related data in a form.This element is used with <legend>
element which provide caption for the grouped element
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
HTML frames
HTML frames are used to divide your browser window into multiple sections where each section
can load a separate HTML document. A collection of frames in the browser window is known as
a frameset.
To use frames on a page we use <frameset> tag instead of <body> tag. The <frameset> tag
defines, how to divide the window into frames. The rows attribute of <frameset> tag defines
horizontal frames and cols attribute defines vertical frames.
A <frame> tag is used with <frameset>, and it divides a webpage into multiple sections or frames, and
each frame can contain different web pages.
The ” iframe ” tag defines a rectangular region within the document in which the browser can display a
separate document, including scrollbars and borders. An inline frame is used to embed another
document within the current HTML document.
</iframe>
</iframe>
Script tag
The <script> tag is used to embed a client-side script (JavaScript).The <script> element either
contains scripting statements, or it points to an external script file through the src
attribute.Common uses for JavaScript are image manipulation, form validation, and dynamic
changes of content.
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
Video tag
The <video> tag is used to embed video content in a document, such as a movie clip or other
video streams.
The <video> tag contains one or more <source> tags with different video sources. The browser
will choose the first source it supports.
The text between the <video> and </video> tags will only be displayed in browsers that do not
support the <video> element.
Audio tag
div tag
The <div> tag defines a division or a section in an HTML document.
The <div> tag is used as a container for HTML elements - which is then styled with CSS or
manipulated with JavaScript.
<html><head>
<style>
.myDiv {
border: 5px outset red;
background-color: lightblue;
text-align: center;
}
</style></head>
<body><h1>The div element</h1>
<div class="myDiv">
<h2>This is a heading in a div element</h2>
<p>This is some text in a div element.</p>
</div>
1. Nav Tag
The <nav> tag helps in defining a set of navigation links. It helps create a section of a website that
contains navigation links
The <mark> element highlights a particular text that is of special interest to the user in an HTML
document.
8. vector graphics
It can be used to draw graphics with various shapes and colors via scripting usually JS. Vector
graphics are scalable, easy to create and edit. It also supports interactivity and animation.
<svg id = "svgelem" height = "200" xmlns = "http://www.abc.org/2000/svg">
<circle id = "redcircle" cx = "50" cy = "50" r = "50" fill = "red" />
</svg>
Unit 1
Chapter 2: Javascript
JavaScript is an object-based, client-side scripting language that you can use to make Web pages more
dynamic.
Object Based
Object based means that JavaScript can use items called objects. However, the objects are not class based
(meaning no distinction is made between a class and an instance); instead, they are just general objects.
Client Side
Client side means that JavaScript runs in the client (software) that the viewer is using, rather than on the
Web server of the site serving the page. In this case, the client would be a Web browser.
Server-Side Languages
A server-side language needs to get information from the Web page or the Web browser, send it to a
program that is run on the host’s server, and then send the information back to the browser. Therefore, an
intermediate step must send and retrieve information from the server before the results of the program are
seen in the browser.
A server-side language often gives the programmer options that a client-side language doesn’t have, such
as saving information on the Web server for later use, or using the new information to update a Web page
and save the updates
Client-Side Languages
A client-side language is run directly through the client being used by the viewer. In the case of JavaScript,
the client is a Web browser. Therefore, JavaScript is run directly in the Web browser and doesn’t need to
go through the extra step of sending and retrieving information from the Web server.
With a client-side language, the browser reads and interprets the code, and the results can be given to the
viewer without getting information from the server first. This process can make certain tasks run more
quickly
Scripting Language
A scripting language doesn’t require a program to be compiled before it is run. JavaScript runs in the
browser by being added into an existing HTML document. You can add special tags and commands to
the HTML code that will tell the browser that it needs to run a script. Once the browser sees these special
tags, it interprets the JavaScript commands and will do what you have directed it to do with your code.
For example, the following code adds some JavaScript to an HTML file that writes some text onto
the Web page. Notice the addition of tags.
Understanding Variables(Primitives)
A variable represents or holds a value. The actual value of a variable can be changed at any time.
Using variables offers several benefits:
● They can be used in places where the value they represent is unknown when the code is written.
● They can save you time in writing and updating your scripts.
● They can make the purpose of your code clearer
Declaring Variables
To declare text as a variable, you use the var keyword, which tells the browser that the text to follow will
be the name of a new variable:
var variablename;
For example, to name your variable coolcar, the declaration looks like this:
var coolcar;
Assigning Values to Variables
To assign a value to a variable, you use the JavaScript assignment operator, which is the equal to (=)
symbol.
var variablename=variablevalue;
Understanding Variable Types
So far, you’ve seen examples of variable values that are numbers. In JavaScript, the variable values, or
types, can include number, string, Boolean, and null.
1. Number
Number variables are just that—numbers. JavaScript does not require numbers to be declared as integers,
floating-point (decimal) numbers, or any other number type. Instead, any number is seen as just another
number, whether it is 7, –2, 3.453, or anything else.
var variablename=number;
Here are some examples:
var paycheck=1200;
var phonebill=29.99;
var savings=0;
var sparetime=-24.5;
String
String variables are variables that represent a string of text. The string may contain letters, words, spaces,
numbers, symbols, or most anything you like.
var variablename="stringtext";
some examples of string variables:
var mycar="Corvette"; var oldcar="Big Brown Station Wagon";
var mycomputer="Pentium 3, 500 MHz, 128MB RAM";
var oldcomputer="386 SX, 40 mHz, 8MB RAM";
var jibberish="what? cool! I am @ home 4 now. (cool, right?)";
The addition operator enables you to place a variable before, after, or even into the middle of a string
<script type="text/javascript">
var mycar="Corvette";
document.write("I like driving my "+mycar+" every day!");
</script>
Boolean
A Boolean variable is one with a value of true or false.
Here are examples:
var JohnCodes=true;
var JohnIsCool=false;
Instead of using the words true and false, JavaScript also allows you to use the number 1 for true and the
number 0 for false, as shown here:
var JohnCodes=1;
var JohnIsCool=0;
Null
Null means that the variable has no value. It is not a space, nor is it a zero; it is simply nothing. If you
need to define a variable with a value of null, use a declaration like this:
var variablename=null;
JavaScript Operators
An operator is a symbol or word in JavaScript that performs some sort of calculation, comparison, or
assignment on one or more values.
Common calculations include finding the sum of two numbers, combining two strings, or dividing two
numbers. Some common comparisons might be to find out if two values are equal or to see if one value
is greater than the other
1. Mathematical These operators are most often used to perform mathematical calculations on two values.
The mathematical operators will probably be the most familiar to you. They use symbols such as +, –,
and *.
2. Assignment These operators are used to assign new values to variables. One of the assignment operators
is the symbol =.
3. Comparison These operators are used to compare two values, two variables, or perhaps two longer
statements. They use symbols such as > (for “is greater than”) and < (for “is less than”).
4. Logical These operators are used to compare two conditional statements (or to operate on one statement)
to determine if the result is true and to proceed accordingly. They use symbols such as && (returns true
if the statements on both sides of the operator are true) and || (returns true if a statement on either side of
the operator is true).
5. Bitwise These are logical operators that work at the bit level (ones and zeros). They use symbols like <<
(for left-shifting bits) and >> (for right-shifting bits).
6. Special These are operators that perform other special functions of their own.
1. Mathematical operator
i. The Addition Operator (+)
Variables for Addition Results One use of the addition operator is to add two numbers to get the
mathematical result. When adding numerical values, you often assign the result to a variable and use the
variable later to make use of the result.
var thesum=4+7;
window.alert(thesum);
The result is an alert that says 11.
Type Conversions in Addition Calculations
It is important to note how JavaScript performs type conversion when working with the mathematical
operators.
var num1=4.73;
var num2=7;
var thesum=num1+num2;
window.alert(thesum);
The result is an alert that says 11.73
If you add a number and a string, the result will come out as though you had added two strings.
var num1=4;
var num2="7";
var thesum=num1+num2;
window.alert(thesum);
When they are added, they are added like strings (47)
ii. The Subtraction Operator (–)
var theresult=10-3;
window.alert(theresult);
Example 2
var num1=10;
var num2=3;
var theresult=num1-num2;
window.alert(theresult);
iii. The Multiplication Operator (*)
The multiplication operator is used to multiply the value on its right side by the value on its left side.
var num1=4;
var num2=5;
var thetotal=num1*num2;
window.alert(thetotal);
iv. The Division Operator (/)
The division operator is used to divide the value on its left side by the value on its right side
var num1=10;
var num2=2;
var theresult=num1/num2;
window.alert(theresult);
Type Conversions in Division Calculations:
var num1=3;
var num2=4;
var theresult=num1/num2;
window.alert(theresult);
The result in this case is 0.75, which is what you see in the alert box
v. The Modulus Operator (%)
The modulus operator is used to divide the number on its left side by the number on its right side, and
then give a result that is the integer remainder of the division.
var num1=11;
var num2=2;
var theresult=num1%num2;
window.alert(theresult);
vi. The Increment Operator (++)
The increment operator can be used on either side of the value on which it operates. It increases the value
it is operating on by 1, just like adding 1 to the value. The actual result depends on whether the operator
is used before or after the value it works on, called the operand.
The Increment Operator Before the Operand : When the increment operator is placed before the
operand, it increases the value of the operand by 1, and then the rest of the statement is executed. Here is
an example
var num1=2;
var theresult=++num1
Here the increment occurs first because the increment operator is in front of the operand. So, the value of
num1 is set to 3 (2+1) and is then assigned to the variable theresult, which gets a value of 3.
The Increment Operator After the Operand : If you place the increment operator after the operand, it
changes the value of the operand after the assignment.
var num1=2;
var theresult=num1++;
example
<script type="text/javascript">
num1=2;
result= ++num1;
alert("num1= "+num1+" result= "+result); // num1= 3 result= 3.
num1=2;
result= num1++;
alert("num1= "+num1+" result= "+result); // num1=3 result=2
</script>
vii. The Decrement Operator (– –)
The decrement operator works in the same way as the increment operator, but it subtracts 1 from the
operand rather than adding 1 to it.
If you place the decrement operator before the operand, the operand is decremented, and then the
remainder of the statement is executed.
Here is an example:
var num1=2;
var theresult=--num1;// num1=1 theresult=1
When you place the operator after the operand, as in the next example, the rest of the statement is executed
and the operand is decremented afterward:
var num1=2;
var theresult=num1--; // num1= 1 theresult=2
viii. The Unary Negation Operator (–)
Unary negation is the use of the subtraction sign on only a single operand.
var negnum=-3;
This defines a variable with a value of negative 3. Basically, the operator tells the browser that the 3 is
“not positive,”.
The AND Operator (&&) The logical operator AND returns true if the comparisons on both sides of the
&& operator are true
ii. The OR Operator (||)
The logical operator OR returns true if the comparison on either side of the operator returns true. So,
for this to return true, only one of the statements on one side needs to evaluate to true.
6. Special Operators
Objects in javascript
Objects are useful because they give you another way to organize things within a script. Rather than
having a bunch of similar variables that are out there on their own, you can group them together under an
object
Creating Objects
<script>
function car(seats,engine,theradio)
{
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
</script>
In this code the function takes three parameters.
The keyword this in JavaScript is used to represent the current object being used, or “this object,”.
Using new keyword you can create instances of object.
Expression in javascript
An expression is a block of code that evaluates to a value. A statement is any block of code that
is performing some action.
Primary expressions
this is used to refer to the current object; it usually refers to the method or object that calls it.
this is used either with the dot operator or the bracket operator.
this['element']
this.element
Grouping operator
a*b-c
a * (b - c)
a * b - c applies the multiplication operator first and then evaluates the result of the multiplication
with - b, while a * (b - c) evaluates the brackets first.
new
new creates an instance of the object specified by the user and has the following prototype.
super calls on the current object’s parent and is useful in classes to call the parent object’s
constructor.
Screen output
1. Using innerHTML
To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML
content:
innerHTML refers to the contents between a tag's beginning and end
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Output
2. Using document.write()
<script>
document.write(5 + 6);
</script>
<script>
window.alert(“welcome”);
Window.alert(5+6);
</script>
In JavaScript, the window object is the global scope object. This means that variables, properties,
and methods by default belong to the window object.
4. Using console.log()
For debugging purposes, you can call the console.log() method in the browser to display data.
<script>
console.log(5 + 6);
</script>
1. Keydown:Keydown happens when the key is pressed down,and auto repeats if the key is
pressed down for long.
2. Keypress: This events is fired when an alphabetics,numeric,or punctuations key is pressed
down.
3. Keyup: keyup happens when the key is released.
Example:
Document.addEventListener(‘Keydown’,(event)=> {
var name=event.key;
var code=event.code;
//alert the key name & key code on keydown
Alert(‘Key pressed ${name}\n Key code value:${code}’);
},false);
Event.code The keyboard event has two important properties: key and code. The key property
returns the character that has been pressed whereas the code property returns the physical key
code.
For example, if you press
the z characterkey,the event.key returns z and event.code returns KeyZspecifies exactly which
key is pressed. For instane ,most keybord have two shift keys;on the left and on the right side.
The event.code tells us exactly which one was pressed,and event.key is responsible for
meaning of the key: what it is(a “shift”).
addEventListener:
An event listener in JavaScript is a wat that you can wait user interaction like a click or key
press and the run some code whenever that action happens.
One common use case for event listeners is listening for click events on a button.