Java Script
Java Script
JAVA SCRIPTING
CONDITIONAL STATEMENTS
Mentor: Shanthini
Conditional Statements
Below are the conditionals statements in Javascript:
• if statement - execute some code only if a specified condition is true
• if...else statement - execute some code if the condition is true and another
code if the condition is false
• if...else if....else statement - use this statement to select one of many
blocks of code to be executed
• switch statement - use this statement to select one of many blocks of
code to be executed
If statements
Use the ‘if’ statement to execute some code only if a specified condition is true.
Syntax:
if (condition)
{
code to be executed if condition is true
}
If Statement - Example
<script type="text/javascript">
//Write a "Good morning" greeting if the time is less than 10’o Clock
if (time<10)
{
document.write("<b>Good morning</b>");
}
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
If Else Statement- Example
<script type="text/javascript">
//write a "Good morning" greeting if the time is less than 14
var d=new Date();
var time=d.getHours();
if (time<14)
{
document.write("<b>Good afternoon</b>");
}
else
{
document.write("<b>Good evening</b>");
}
If...else if...else Statement
Use the if....else if...else statement to select one of several blocks of code to be executed.
Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
If...else if...else Statement - Example
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Hello World!</b>");
}
</script>
Math.random()
<html>
<body>
<script type="text/javascript">
var r=Math.random();
if (r>0.5)
{
document.write(“This is a value greater than 0.5");
}
else
{
document.write(“ This is some other value");
}
</script>
</body>
</html>
Java Scripting: Switch Case
• Switch statement is used as an alternate to multiple if .. else statements
• Use the Switch Case statement to pick a block of code to execute
• The Switch statement uses strict comparisons (===), values must be the same type to match
Syntax:
switch(expression)
{
case value: expression;
break;
case value: expression;
break;
default: Expression;
}
Switch Case - Example
var my_day=new Date();
switch (my_day.getDay())
{
case 0: console.log("Today is Sunday"); break;
case 1: console.log("Today is Monday"); break;
case 2: console.log("Today is Tuesday"); break;
case 3: console.log("Today is Wednesday"); break;
case 4: console.log("Today is Thursday"); break;
case 5: console.log("Today is Friday"); break;
case 6: console.log("Today is Saturday"); break;
default: console.log("value of i is not equal to any given days"); break;
}
JavaScript Switch vs If Statement
Switch Case: If Else Case:
• You are comparing multiple possible • You want to test for the truthiness of an
conditions of an expression and the expression expression.
itself is non-trivial.
• You only have a single affirmative test.
• You have multiple values that may require the
same code. • You need to evaluate different expressions for
each branch
• You have some values that will require
essentially all of another value's execution,
plus only a few statements.
Object Oriented Programming
Concepts of Object Oriented Programming:
• Object
• Classes
• Encapsulation
• Inheritance
Objects
Object
• Is an unique entity which
JavaScript is an Object based language
contains property and methods
• The characteristics of an Object are called
as Property, the actions are called as
methods.
• An Object is an instance of a class
Example of an object is a Person
Whenever an object is created in JavaScript, its corresponding functions are loaded into memory.
So, a new copy of the function is created on each object creation.
Classes
Class
• Is a blueprint of an Object
• From class we can create object instances
• One class can have many objects
• Class is a template while Object are instances of
the class
• Creating an object instance from a class is
called as instantiation
• When creating the object, an instance
is instantiated from the class.
How can we create Objects
Object can be created by three ways in JavaScript:
• Object Literal
• Object Constructor - create many objects for same class
• Object.create() method - Create instance of Object
Object creation using Object Literal
• Syntax:
object={property1:value1, property2:value2 .....propertyN:valueN}
• property and value is separated by : (colon).
• Example:
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
• Output
102 Shyam Kumar 40000
Object creation using Object.create() method
• Syntax:
var objectname=new Object();
• New keyword is used to create object.
• Example:
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
• Output:
101 Ravi 50000
Object creation using Object Constructor
• To create an object using Example:
constructor, create function with <script>
arguments. function emp(id,name,salary){
this.id=id;
• Each argument value can be this.name=name;
assigned in the current object by this.salary=salary;
using this keyword. }
e=new emp(103,"Vimal Jaiswal",30000);
• We use this keyword to refer to the document.write(e.id+" "+e.name+" "+e.salary);
current object. </script>
Output:
103 Vimal Jaiswal 30000
Defining method in JavaScript Object
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
this.changeSalary=changeSalary;
function changeSalary(otherSalary){
this.salary=otherSalary; }
}
e=new emp(103,"Sonoo Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary); Output:
103 Sonoo Jaiswal 30000
e.changeSalary(45000); 103 Sonoo Jaiswal 45000
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>
Prototype
• JavaScript is a prototype based language
• In JavaScript object acquire properties and features from another object.
• Each object contains a prototype object.
• Whenever a function is created the prototype property is added to that function automatically
• It holds Constructor property
Syntax:
ClassName.prototype.methodName
Here, all the objects share the same function. This ignores the requirement of creating a new
copy of function for each object. Thus, the functions are loaded once into the memory and use it
multiple times
Prototype Example
<script>
function Employee(firstName,lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
Employee.prototype.fullName=function()
{
return this.firstName+" "+this.lastName;
}
var employee1=new Employee("Martin","Roy");
var employee2=new Employee("Duke", "William"); Output:
document.writeln(employee1.fullName()+"<br>"); Martin Roy
document.writeln(employee2.fullName()); Duke William
</script>
Constructor
• Constructor method is a special method Example:
which is used to initialize and create an <script>
object. class Employee {
• It is called when memory is allocated for constructor() {
an object. this.id=101;
• The constructor keyword is used to this.name = "Martin Roy";
declare a constructor method. }
}
• The class can contain one constructor
method only. var emp = new Employee();
document.writeln(emp.id+" "+emp.name);
• JavaScript allows us to use parent class
</script>
constructor through super keyword.
Output:
101 Martin Roy
Constructor – Super Keyword
The super keyword is used to call the parent class constructor. Let's see an example.
<script>
class CompanyName
{
constructor()
{
this.company=“Verzeo";
}
}
class Employee extends CompanyName {
constructor(id,name) {
super();
this.id=id;
this.name=name;
}
} Output:
var emp = new Employee(1,"John"); 1 John Verzeo
document.writeln(emp.id+" "+emp.name+" "+emp.company);
</script>
Inheritance
• When a object or a class inherits the property of
another object.
• The object which inherits the property from
another object is called as child object
• Use extends keyword to inherit
• Inheritance helps in code Reuse
• JavaScript Object inherits Object i.e. certain
features (property and methods)
• Maintains an IS-A relationship.
Example of Inheritance
<script>
class Bike
{
constructor()
{
this.company="Honda";
}
}
class Vehicle extends Bike {
constructor(name,price) {
super();
this.name=name;
this.price=price;
}
}
var v = new Vehicle("Shine","70000");
document.writeln(v.company+" "+v.name+" "+v.price);
</script>
<script>
class Student
Encapsulation {
constructor()
{
• The process of wrapping property and var name;
var marks;
function within a single unit }
getName()
• Use var keyword to make data members {
private. return this.name;
}
setName(name)
• Use setter methods to set the data and getter {
methods to get that data. this.name=name;
}
getMarks()
{
return this.marks;
}
setMarks(marks)
{
this.marks=marks;
}
}
var stud=new Student();
stud.setName("John");
stud.setMarks(80);
document.writeln(stud.getName()+" "+stud.getMarks());
</script>
Polymorphism
• Polymorphism allows to perform a single Example:
action in different formats
<script>
• We can call the same method on different class A
JavaScript objects {
display()
{
document.writeln("A is invoked");
}
}
class B extends A
{
}
var b=new B();
b.display();
</script>
Output:
A is invoked
Questions?