Output:
a is equal to 20
Nested if statement:
«A nnested if is the target of another if or else.
* Nested if statements mean an if statement inside an if statement.
Syntax:
if condition!
{
// Executes when condition! is true
if condition2
{
/ Executes when condition? is true
}
}
9423034909 4Basies of JavaScript Programming
17 using if statement
if(v1==50) {
1/if condition is true then
JI check the following
if(v2==60)
{
/1f condition is true
// then display the following
document.write("Value of v1 is 50 and v2 is 60");
}
}
€ > S O Fie| c/zambarepaycss%.. 1 t OO FX:
JavaScript do while loop:
1) The JavaScript do-while loop iterates the elements for an infinite number of times like a
while loop.
2) But, code is executed at least once whether the condition is true or false.
9423034909
18,Basics of JavaScript Programming,
3) The syntax of do while loop is given below.
Syntax:
do
{
IIcode to be executed
}
while (condition);
Example:
Example of do while loops/TITLE>
Output:
@ Example cf continue statement x
€ > S O File| cyzambarevaycss%a0rr.. 1 Oh FA
A loop which will skip the step where i=
The number is 0
The number is 1
The number is 2
The number is 4
‘The number is 5
The number is 6
‘The number is 7
‘The number is 8
‘The number is 9
The number is 10
1.8 Querying and setting properties and deleting properties:
Property Attributes:
1) All properties have a name.
9423034909
2Basies of JavaScript Programming
2) In addition they also have a value.
3). The value is one of the property's attributes.
Adding New Properties:
1) You can add new properties to an existing object by simply giving it a value.
2) Assume that the person object already exists - you can then give it new properties:
Example:
person.nationality = "Indian";
Deleting Properties:
1) The delete keyword deletes a property from an object:
Example
var person=
firstName:"Dattatray",
lastName:"Zambare"
age:33,
eyeColor:"black"
delete person.age;
or
delete personf"age"];
2) The delete keyword deletes both the value of the property and the property itself. After
deletion, the property cannot be used before it is added back again.
3) The delete operator is designed to be used on object properties. It has no effect on variables or
functions.
4) The delete operator should not be used on predefined JavaScript object properties. It can crash
your application.
Q. Write a Java script to create person object with properties firstname, lastname, age,
eyecolor, delete eyecolor property and display remaining properties of person object.
Answer:
Output:
€ > SO File | CyZambareDB/Css%20Fr....
After delete John Doe 50 undefined
Property getters and setters:
1) The accessor properties. They are essentially functions that work on getting and setting a
value.
2) Accessor properties are represented by “getter” and “setter” method:
they are denoted by get and set:
let obj =
{
get propName() [
1/ getter, the code executed on getting obj.propName
. In an object literal
1
set propName(value) {
// setter, the code executed on setting obj.propName = value
}
h
3) An object property is a name, a value and a set of attributes. The value may be replaced by
one or two methods, known as setter and a getter
4) When program queries the value of an accessor property, Javascript invoke getter method
(passing no arguments). The return value of this method becomes the value of the property
access expression.
5) When program sets the value of an accessor property. Javascript invoke the setter method,
passing the value of right-hand side of assignment. This method is responsible for setting
the property value.
* If property has both getter and a setter method, it is read/write
9423034909 23Basies of JavaScript Programming
* property.
* If property has only a getter method , it is read-only property.
If property has only a setter method , itis
write-only property.
6) getter works when obj.propName is read, the setter — when it is assigned.
Example:
9423034909
Stitle> property getters and setterss/title>
24Basies of JavaScript Programming
Output:
a
€ > GO Fie| C/ZambareDeycss%20rr.. 2? tr | Th FA
Car color:blue Car Make: Toyota
Car colored Car Make: Audi
OR.
Q. Explain setter and getter properties in JavaScript with the help of suitable example.
Property getters and sette
1. The accessor properties. They are essentially functions that work on getting and setting a value.
2. Accessor properties are represented by “getter” and “setter” methods. In an object literal they are
denoted by get and set.
get: It is used to define a getter method to get the property value.
set: It is used to define a setter method to set / change the property value
let obj
get propName()
{
1! getter, the code executed on getting obj.propName
}
set propName(value)
{
setter, the code executed on setting obj.propName = value
}
K
3. An object property is a name, a value and a set of attributes. The value may be replaced by one or
two methods, known as setter and a getter.
Example of getter and Setter:
property getters and setters
1) Itdisplays the confirm dialog box. Ithas message with ok and eancel buttons.
2) Returns Boolean indicating which button was pressed
Synta
Example :
alert():
low.confirm("sometext");
"delete record” onclick="msg()"/>
It displays the alert dialog box. It has message with ok button.
Syntax:
window.alert("some alert text");
Example :
9423034909