CSS Unit 1. Basics of JavaScript Programming
CSS Unit 1. Basics of JavaScript Programming
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
1
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*What is JavaScript?
JavaScript is the Programming Language for the Web (web-based applications and web
browsers).
JavaScript can update and change both HTML and CSS.
JavaScript can calculate, manipulate and validate data.
JavaScript is a text-based programming language used both on the client-side and server-
side that allows you to make web pages interactive.
HTML and CSS are languages that give structure and style to web pages, JavaScript gives
web pages interactive elements that engage a user.
Example of JavaScript: the search box on Amazon.
2
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Features of JavaScript:
1. Validating User’s Input:
It has the capability to validate user input for errors and also saves time.
If the user leaves a required field empty or the information is incorrect, JavaScript checks for
them before sending the data over to the server.
3. Greater Control:
JavaScript provides greater control to the browser rather than being completely dependent
on the web servers.
4. Platform Independent:
Since browsers interpret JavaScript, it can run on Windows, Macintosh, and other Netscape-
supported systems.
8. Let / Const:
JavaScript has introduced the keywords ‘let’ and ‘const’ that are available to replace ‘var’.
Unlike ‘var’, they are important due to their blocked scope i.e we can only access them in the
block we defined them in. Whereas ‘var’, even if we initialize it inside a function, we can
access it outside of the function.
3
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*JavaScript Objects:
Object:
Object is a real time entity. i.e. real-world object.
Example: car, person, student, etc.
JavaScript objects are containers for named values called properties or methods.
In real life, a car is an object.
Ex: var car; (“var” is used to declare the variable)
Property:
A property is an association between a name and a value.
Name, color, model these are the properties of a car object.
Ex: car.type=”skoda”;
car.color=”black”;
car.model=”rapid”;
Method:
A method is an action which an object is able to perform.
Start, stop these are the methods of car object.
Ex: car.start();
car.stop();
Program:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<script>
var car = {type:"skoda", model:"rapid", color:"black"}; //object creation
document.write("The car type is " + car.type+"</br>"); // display some data from the object
document.write("The car model is " + car.model+"</br>");
document.write("The car color is " + car.color+"</br>");
</script>
</body>
</html>
Output:
The car type is skoda
The car model is rapid
The car color is black
4
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Dot syntax:
To access an object, property, or method, its reference must include every object that
contains it, separated by a dot. This is called the "dot syntax".
car.start();
Output: 2
5
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Sample Program:
For JavaScript coding follow the steps below:
Use Tryit editor OR
Open the Notepad, type the program and save the file as filename.html.
It creates the browser file, to run the program open the browser file.
For writing program use the <script> tag.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables</p>
<p id="demo"></p>
<script>
var x = 17; //declaration of variable and assigning value
var y = 6;
var z = x + y; //variable z stores the result
document.write("Addition of " + x +" and " + y +" is = " + z + ""); //document.write is used to
display the message or output (result)
</script>
</body>
</html>
Output:
JavaScript Variables
In this example, x, y, and z are variables.
Addition of 17 and 6 is = 23
6
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
7
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*Operators in JavaScript:
1. Assignment Operators:
An assignment operator assigns a value to its left operand based on the value of its right
operand. The simple assignment operator is equal (=), which assigns the value of its right
operand to its left operand.
Ex: x = y; //assigns the value of y to x
2. Arithmetic Operators:
a. addition (+) e. remainder (%)
b. subtraction (-) f. increment (++)
c. multiplication (*) g. decrement (--)
d. division (/) h. Exponentiation operator (**) (power)
8
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Output:
Addition of 4 and 2 is = 6
Subtraction of 4 and 2 is = 2
Multiplication of 4 and 2 is = 8
Division of 4 and 2 is = 2
Remainder of 4 and 2 is = 0
Power of 4 raise to 2 is = 16
Increment value: 6
Decrement value: 5
9
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
3. Comparison Operators:
A Comparison operator compares its operands and returns a logical value based on whether the
comparison is true. The operands can be numerical, string, logical, or object values.
4. Logical Operators:
Logical operators are typically used with Boolean (logical) values; when they are, they return
a Boolean value. However, the && and || operators actually return the value of one of the
specified operands.
10
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
<!DOCTYPE html>
<html> <head>
<title>Javascript Conditional Operator</title>
</head>
<body>
<h1>Performing Conditional Operations </h1>
<script>
var age=17;
var status = (age >= 18) ? 'adult' : 'minor'; //ternary or conditional operator
document.write(status);
</script> </body> </html>
Output: minor
6. Delete Operator:
The delete operator deletes an object, an object's property, or an element at a specified index
in an array.
<!DOCTYPE html>
<html> <body>
<h2>JavaScript Delete Operator</h2>
<script>
var person = {firstname:"John", lastname:"Doe", age:50, eyecolor:"blue"};
delete person.age; //deletes the age property of person object
document.write(" The age of john is "+person.age+"");
</script> </body>
</html>
Output:
The age of john is undefined //output is undefined as we delete the person.age
11
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
7. String Operator:
In addition to the comparison operators, which can be used on string values, the
concatenation operator (+) concatenates two string values together, returning another string
that is the union of the two operand strings.
Output:
hi sir how are you?
alphabet
12
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*Conditional Statements:
Conditional statements are used to perform different actions based on different conditions.
In JavaScript we have the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
IF statement:
Use the if statement to specify a block of JavaScript code to be executed if a condition is
true.
Syntax: if (condition) {
//block of code to be executed if the condition is true
}
IF..ELSE statement:
Use the else statement to specify a block of code to be executed if the condition is false.
Syntax: if (condition) {
//block of code to be executed if the condition is true
} else {
//block of code to be executed if the condition is false
}
IF..ELSEIF statement:
Use the else if statement to specify a new condition if the first condition is false. i.e multiple
conditions.
Syntax: if (condition1) {
//block of code to be executed if condition1 is true
} else if (condition2) {
//block of code to be executed if the condition1 is false and condition2 is true
.
.
} else {
//block of code to be executed if the condition1 is false and condition2 is false
}
13
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Output:
Displays the greetings as per the time in clock (of PC).
14
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
15
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Switch..Case Statement:
The switch statement is used to perform different actions based on different conditions.
Use the switch statement to select one of many code blocks to be executed.
Syntax: switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Program on Switch Case:
Program to display the current day using switch case statement.
<!DOCTYPE html>
<html>
<body>
<h> Switch case statement</h><br />
<script>
var day;
switch (new Date().getDay())
{
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
16
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.write("Today is "+ day+"");
</script> </body> </html>
17
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*Loop Statements:
FOR Loop:
A for loop repeats until a specified condition evaluates to false.
Syntax: for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
Program on FOR loop:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Loop</h2>
<script>
var i;
for (i = 0; i < 5; i++)
{
document.write(""+i+"<br>");
}
</script>
</body>
</html>
Output:
0
1
2
3
4
18
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
<body>
<h2>JavaScript For Loop</h2>
<p id="demo"></p>
<script>
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var i, len;
for (i = 0, len = cars.length; i < len; i++)
{
document.write(""+cars[i]+"<br>");
}
</script>
</body>
</html>
Output:
BMW
Volvo
Saab
Ford
For..in Loop:
The JavaScript for/in statement loops through the properties of an object.
Program on For..in loop:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript for/in Loop to access property of an object</h2>
<script>
var person = {fname:"John", lname:" Doe", age:25};
var x;
for (x in person)
{
document.write(""+person[x]+"");
}
</script>
</body>
</html>
Output: JohnDoe25
19
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
For..of Loop:
The JavaScript for/of statement loops through the values of an iterable objects or an array.
for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps,
NodeLists, and more.
20
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
While Loop:
The while loop loops through a block of code as long as a specified condition is true.
Syntax: while (condition)
{
// code block to be executed
}
Program on while loop:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript While Loop</h2>
<script>
var i = 0;
while (i < 5)
{
document.write(""+i+"<br>");
i++;
}
</script>
</body>
</html>
Output:
0
1
2
3
4
Do..While Loop:
The do/while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition is
true.
Syntax: do
{
// code block to be executed
}
while (condition);
21
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Output:
0
1
2
3
4
Continue Statement:
The continue statement breaks one iteration (in the loop), if a specified condition occurs,
and continues with the next iteration in the loop.
22
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
if (i === 3) { continue; }
document.write(""+i+"<br>");
}
</script>
</body>
</html>
Output:
A loop which will skip the step where i = 3.
0
1
2
4
23
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
*JavaScript Accessors:
JavaScript getters and setters:
Getters are used to get the value of the property of an Object.
Setters are used to set the value to the property of an Object.
Get:
The get syntax binds an object property to a function that will be called when that property is
looked up.
Program on get:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters allow you to get and set properties via methods.</p>
<p>This example uses a lang property to get the value of the language property.</p>
<script>
var person = { firstName: "John", lastName : "Doe", language : "English",
get lang()
{
return this.language;
}
};
document.write("The language is: "+person.lang+"");
</script>
</body>
</html>
Output:
The language is: English
24
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Set:
The set syntax binds an object property to a function to be called when there is an attempt
to set that property.
Program on Set:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters allow you to get and set properties via methods.</p>
<p>This example uses a lang property to set the value of the language property.</p>
<script>
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
set lang(value) {
this.language = value;
}
};
person.lang = "English";
document.write("The language set to "+person.language+"");
</script>
</body>
</html>
Output:
The language set to English
25
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
Annexure
26
CM5I CSS 22519 Nibe Sir, CM Dept.
Padmashri Dr. Vitthalrao Vikhe Patil Institute of Technology & Engineering POLYTECHNIC Loni
2 Marks Questions:
1. State the use of dot syntax in JavaScript with the help of suitable example.
2. List and explain two Logical operators in JavaScript.
3. List any four features of Java script. (Win 2019)
4. List the comparison operators in Java script. (Win 2019)
5. Write Java script to create person object with properties firstname, lastname,
age, eye color, delete eye color property and display remaining properties of person
object. (Win 2019)
27