JavaScript Tech Move
JavaScript Tech Move
v
Displaying date and time
e
o
Displaying pop-up windows and dialog boxes (like an alert dialog box,
confirm dialog box and prompt dialog box)
Displaying clocks etc.
M
JS syntax:
Te c
<script>
h
document.write("Hello JavaScript by JavaScript");
</script>
Java Script Features:
There are following features of JavaScript
All popular web browsers support JavaScript as they provide built-in
execution environments
JavaScript follows the syntax and structure of the C programming
language. Thus, it is a structured programming language
JavaScript is a weakly typed language, where certain types are
implicitly cast (depending on the operation)
JavaScript is an object-oriented programming language that uses
prototypes rather than using classes for inheritance
It is a light-weighted and interpreted language
It is a case-sensitive language
JavaScript is supportable in several operating systems including,
Windows, macOS, etc
It provides good control to the users over the web browsers.
Cl ie n t - Si d e JavaScript:
Client-side JavaScript is the most common form of the language. The
script should be included in or referenced by an HTML document for
the code to be interpreted by the browser
It means that a web page need not be a static HTML, but can include
programs that interact with the user, control the browser, and
dynamically create HTML content
The JavaScript client-side mechanism provides many advantages
over traditional C G I server-side scripts. For example, you might use
JavaScript to check if the user has entered a valid e-mail address in a
form field .
E xamples
var a =40; //holding numbe
var b = " R ahul" ; //holding strin g
Primitive Data Type: There are five types of primitive data types in
JavaScript. They are as follows
String: R epresents se q uence of characters e.g. "hello
N umber: R epresents numeric values e.g. 10
There are some rules while declaring a JavaScript variable (also known
as identifiers)
N ame must start with a letter (a to z or A to Z ), underscore( _ ), or
dollar( $ ) sign
After first letter we can use digits ( 0 to 9 ), for example value 1
JavaScript variables are case sensitive, for example x and X are
di ff erent variables .
E xamples
var x = 10
var _ value = "sonoo" ;
Syntax :
v
<script>
e
o
function abc() {
M
c
</script >
h
O R
<script>
Te
If( 10 < 1 3 ) {
</script>
E xamples :
<script>
function a() {
document.writeln(data);
function b() {
document.writeln(data);
b();
</script >
JavaScript O perat o rs :
Logical Operator
Assignment Operator
Special Operator s
Addition( + ): 10+20 = 3
Subtraction(-) : 20 - 10 = 1
Multiplication( * ): 10 * 20 = 20
Division(/): 20 / 10 =
Modulus ( R emainder)( % ): 20 % 10 =
Increment( ++ ): var a =10; a ++; N ow a = 1
Decrement(--): var a =10; a-- ; N ow a = 9
B itwise N OT( ~ ): ( ~ 10 ) = - 1
Assign( = ): 10+10 = 2
Add and assign( += ): var a =10; a +=20; N ow a = 3
Subtract and assign(- = ): var a =20; a- =10; N ow a = 1
Multiply and assign( * = ): var a =10; a * =20; N ow a = 20
Divide and assign(/ = ): var a =10; a/ =2; N ow a =
Modulus and assign( % = ): var a =10; a % =2; N ow a = 0
Syntax:
if(expression) {
//content to be evaluate d
E xample :
<script>
var a =20 ;
if(a> 10 ) {
</script >
Output :
Syntax:
if(expression) {
else {
E xample :
<script>
var a =20 ;
if(a % 2==0 ) {
else {
</script >
Output :
a is even numbe r
Syntax:
if(expression 1 ) {
else if(expression 2 ) {
else if(expression 3 ) {
else {
E xample :
<script>
var a =20 ;
if(a ==10 ) {
else {
</script >
Output :
a is e q ual to 2 0
Syntax:
switch(expression) {
case value 1 :
code to be executed;
break;
case value 2 :
code to be executed;
break;
..... .
default :
E xample:
<script>
var result;
switch(grade) {
v
case 'A' :
e
o
result = "A G rade";
break;
M
case 'B' :
c
result = " B G rade";
break;
h
case ' C ' :
Te
break;
default :
document.write(result);
</script >
Output :
B G rad e
JavaScript Loo ps : 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.
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 .
Syntax :
code to be execute d
E xample :
<script>
document.write(i + "<br/>" )
</script >
Output :
JavaScript 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 .
Syntax :
while (condition )
code to be execute d
Example :
<script>
var i =11 ;
while (i< =1 5 )
document.write(i + "<br/>");
i ++ ;
</script >
Output :
1 1
1 2
1 3
1 4
1 5
Syntax :
do {
code to be execute d
}while (condition) ;
E xample :
<script>
var i =21 ;
do {
document.write(i + "<br/>");
i ++ ;
} while (i< =2 );
5
</script >
Output :
2 1
2 2
2 3
2 4
2 5
JavaScript For in loop: The JavaScript for in loop is used to iterate the
properties of an object .
Syntax :
Example :
<script>
</script >
Output :
John Doe 2 5
Syntax :
//code to be execute d
<script>
function msg() {
</script>
Output :
Call functio n
<script>
function getcube(number) {
</script>
<form>
</form >
Output :
Clic k
<script>
function getInfo() {
</script>
<script>
document.write(getInfo());
</script >
Output :
<script>
document.writeln(add( 2 , ));
5
</script >
Output :
Syntax :
Example :
<script>
</script >
Output :
Syntax :
xample :
v
E
<script>
e
o
var emp = new Object();
emp.id =101 ;
M
emp.name = " R avi Malik";
c
emp.salary = 0000 ;
h
5
</script >
Te
Output :
E xample :
<script>
function emp(id,name,salary) {
this.id = id;
this.name = name;
this.salary = salary;
</script >
Output :
Syntax: alert(message )
E xample:
function fun( )
</script >
Syntax: confirm(message )
E xample:
let text;
else {
E xample:
if (person ! = null) {
JavaScript E ve n ts:
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, resi z ing a window, etc
E vents are a part of the Document Object Model (DOM) Level 3 and
1 .Mouse events
onclick: When mouse click on an element
onmouseover: When the cursor of the mouse comes over the elemen
onmouseout: When the cursor of the mouse leaves an element
onmousedown: When the mouse button is pressed over the element
onmouseup: When the mouse button is released over the element
onmousemove: When the mouse movement takes place .
2 . Keyboard events
onkeydown & onkeyup: When the user press and then release the key.
3 . Forms events
onfocus: When the user focuses on an element
onsubmit: When the user submits the for
onblur: When the focus is away from a form element
onchange: When the user modifies or changes the value of a form
element .
3 . Window/Document events
onload: When the browser finishes the loading of the page
onunload: When the visitor leaves the current webpage, the browser
unloads it
onresi ze: When the visitor resi zes the window of the browser.
Onclick E vent:
<html>
<body>
< ! - -
function clickevent( )
//-->
</script>
<form>
<input type = "button" onclick = "clickevent()" value = "Who ' s this ? "/>
</form>
</body>
</html >
Onclick E vent:
<html>
<head>
</head>
<body>
< ! - -
function mouseoverevent( )
//-->
</script>
</body >
JavaScript A rra y :
JavaScript array is an object that represents a collection of similar
type of elements
An array can hold many values under a single name, and you can
access the values by referring to an index number
If you have a list of items (a list of car names, for example), storing
the cars in single variables could look like this :
JavaScript array literal: The syntax of creating array using array literal is
given below :
Syntax :
E xample :
<script>
v
var emp = [ "Sonoo"," V imal"," R atan" ] ;
e
o
for (i =0; i<emp.length ; i ++ ) {
document.write(emp [ i ] + "<br/>");
M
c
</script >
Output :
h
Sono
V ima
o
l
Te
R ata n
Syntax :
E xample :
<script>
var i;
emp [ 0 ] = "Arun";
emp [ 2 ] = "John";
document.write(emp [ i ] + "<br>");
</script >
Output :
Aru n
Varu n
Joh n
E xample :
<script>
document.write(emp [ i ] + "<br>");
</script >
Output :
Ja i
V ija y
Smit h
<script>
function validateform() {
return false;
}else if(password.length< 6 ) {
return false;
</script>
<body>
</form >
function matchpass() {
if(firstpassword == secondpassword) {
return true;
else {
return false;
</script>
</form >
JavaScript N umber Validation: Let ' s validate the textfield for numeric
value only. Here, we are using is N a N () function .
<script>
function validate() {
if (is N a N (num)) {
return false;
}else {
return true;
</script>
span><br/>
</form >
o v e
M
Reference :
www.w 3 schools.co m
www.javatpoint.co m
Te c h
googl e
youtube
Th an k You