0% found this document useful (0 votes)
17 views31 pages

java scrpit notes 1

fsfsfsfsfssssssssssssss

Uploaded by

ujef6501
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views31 pages

java scrpit notes 1

fsfsfsfsfssssssssssssss

Uploaded by

ujef6501
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

*******UNIT 1-Basics Of JavaScript Programming*******

What is JavaScript?
• JavaScript is a popular client side scripting language that is supported by all web
browsers. It is used on the client side of web development.
• JavaScript is a dynamic and lightweight programming language.
• It is an interpreted language with object-oriented features.
• JavaScript is known for its powerful capabilities in client-side scripting.
• It is mainly used to enhance user interaction on webpages, making them more lively
and interactive.
• In addition to web development, JavaScript is widely used in game development and
mobile application development.

History of JavaScript
• JavaScript was created by Brendan Eich in 1995.
• Initially, it was called LiveScript, but later its name was changed to JavaScript.

Features of JavaScript
• JavaScript is a scripting language that is based on objects.
• It gives users more control over their web browsers.
• JavaScript can handle dates and time.
• It can detect the user's browser and operating system.
• JavaScript is lightweight.
• JavaScript is a separate language from Java, despite the similar name.
• It is an interpreter-based scripting language.
• JavaScript is case sensitive, meaning that uppercase and lowercase letters are
distinct.
• JavaScript is object-based, providing predefined objects.
• Every statement in JavaScript must end with a semicolon (;).
• The syntax of most JavaScript control statements is similar to that of control
statements in the C programming language

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 1


Advantages of JavaScript
1. Reduced server interaction: JavaScript allows validating user input before sending
it to the server, reducing server traffic and load.
2. Immediate feedback: Users receive instant feedback without needing to wait for a
page reload.
3. Increased interactivity: JavaScript enables the creation of more interactive
interfaces.
4. Richer interfaces: JavaScript can be used to include elements like drag-and-drop
components and sliders, enhancing the user experience.

Limitations of JavaScript
1. File access limitations: Client-side JavaScript does not have the ability to read or
write files due to security reasons.
2. Networking limitations: JavaScript lacks support for networking applications.
3. Lack of multi-threading and multiprocessor capabilities: JavaScript does not
have built-in features for multi-threading or utilizing multiple processors.
4. Performance limitations: JavaScript can be slower compared to other languages,
which may affect performance, especially in complex applications or heavy
computations.
5. Browser compatibility issues: JavaScript may work differently across different web
browsers, requiring developers to test and ensure consistent behavior across
platforms.
6. Limited support for low-level operations: JavaScript doesn't have built-in features
for performing low-level operations like manipulating binary data or directly accessing
computer memory, which some other languages like C or C++ support.

What is Script?
- A Scripting Language is a type of computer programming language used to provide
instructions called scripts, to software application,etc.

Types of Script
1] Client Side Scripting Language
• It executes on client side on their web browser.
• Client-side scripts have the advantage of reducing server demand, allowing web
pages to load quicker.
• Ex. HTML, CSS, JavaScript.
2] Server Side Scripting Language

• It executes on the web server.


• When a client submits an HTTP requests, the server responds by delivering
content.
• One important advantage of server side script is that they are not visible to general
public. Ex. PHP,ASP.NET,Node, JS, Java,Python

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 2


Client Side Scripting VS Server Side Scripting

Client Side Scripting Server Side Scripting

Runs on User’s Web Browser Runs on Web Server

Code is executed on User’s Device Code is executed on the server

Enhances user experience and interactivity Handles server-side processing and data
on the website. manipulation.

Can be used for immediate feedback and Allows for secure handling of sensitive
validation of user inputs. information on the server.

Provides dynamic client-side interactions Generates dynamic content and sends it to


without the need to reload the page. the client for display.

Requires a compatible web browser to Executes on the server and sends the
execute the code. results to the user's browser.

Source code is visible to user. Source code is not visible to user.

It does not provide security for data. It provides more security for data.

Can be used for form validation, Handles database operations, file


animations, and DOM manipulation. processing, and server-side calculations.

Examples include JavaScript, HTML, and Examples include PHP, Python, and Ruby.
CSS.

JavaScript Tag:
• JavaScript is implemented using the <script>...</script> tags in an HTML webpage.
• The <script> tags can be placed anywhere within the webpage, but it is commonly
recommended to put them within the <head> tags.
• The <script> tag notifies the browser to interpret the content between the tags as
JavaScript code
• The basic syntax for including JavaScript code is:
<script language="javascript" type="text/javascript">
// JavaScript code
</script>

• The <script> tag has two important attributes:


1. Language: Specifies the scripting language being used, usually set to
"JavaScript".
2. Type: Indicates the scripting language and should be set to "text/javascript".

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 3


First JavaScript Code:

<html>
<body>
<script language="javascript" type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>

Terms of JavaScript
1] Method

• It is set of instructions performed by an object when it receives a message.


• A JavaScript method has a function definition which is stored as a property value.
2] Property

• A property is a value associated with an object. Every object has its own set of
properties.
• Ex. Window has properties like width, height, background color.
• A property is a key:value pair.
o Key(Property name) is always a string.
o Value(Property value) can be of any data type.
3] Event / Main Event

• JavaScript’s interaction with HTML is handled through events that occur when user
or browser manipulates a page.
• When the page load, it is called an event. When user clicks on button, that click is an
event too.
4] Object-Name

• In JavaScript, an object is an unordered collection of key-value pairs.


• Each key-value pair is called a property.
• The key of a property can be String and the value of property can be of any data
type.
5] Dot-Syntax

• Every object is associated with some property and method. For accessing the
properties and methods of an object we use dot operator.
• Eg document.write(“Hello”); //Accessing method write of document object.

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 4


Variables

• Variable is an entity that are used for storing the value.


• Variable acts as a container for storing the value.
• JavaScript is a dynamically typed language. Hence there is no need for type
declaration.
• We can declare variables using keywords like var, let, or const.

Rules for Declaring Variable

• A variable can have a short name(like x and y) or a more descriptive name(like


age,car_name).
• Variable should be declared by using var,let or const before using it.
• A variable name should begin with alphabets or underscore(_)
• It may combine with number, but should not start with number
• It does not allow any special symbol except underscore (_)
• Variable names are case sensitive.
• Variable name should not be any keyword.
• Variable name should not contain any whitespace.
• JavaScript is loosely typed language.

var VS let VS const

Feature var let const

Scope Functional or global Block scope Block scope


scope

Redecleration Allowed Allowed Not allowed

Reassignment Allowed Allowed Not allowed

Initialization Can be left Can be left Must be initialized at


uninitialized uninitialized the time of
decleration

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 5


Variable Scope:
Scope of variable refers to the visibility of variable within the program.

There are two variable scopes:


1] Local Variable:

- A variable that is defined within a function is called local variable.


- This variable can be accessed throughout the function only where they have
declared.
- Once function is executed the value of variable is destroyed.
2] Global Variable:

- A variable that is defined outside the function is called global variable.


- This variable can be accessed throughout the program including all the functions.
- They can be accessed by all the functions in the program.
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var x=5; //Global Variable

function example(){
var y=10; //Local Variable

document.write("Global X="+x);
document.write("Local Y="+y);
}
example();
</script>
</body>
</html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 6


Data Types in JavaScript:
• JavaScript provides different data types to hold different types of values.
• In JavaScript, every value also has a data type.
• Variables can hold values of different data types.
• JavaScript is a dynamically typed language, which means we do not need to
explicitly define the data type of a variable when declaring it.
• The JavaScript interpreter automatically associates the value with its
corresponding data type.
- There are two types of data types in JavaScript.
1. Primitive data type
2. Non-primitive data type

Primitive data type:

There are five types of primitive data types in JavaScript. They are as follows:

Non-Primitive data type (Derived Data Type):

The non-primitive data types are as follows:

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 7


Comments in JavaScript:
• Comments play an important role in JavaScript programming.
• They describe what's happening inside the code, making it easier for other
developers to understand.
• Comments are helpful because, over time, you may forget the important details of
your own code.
• Comment is a part of documentation.
• JavaScript interpreter ignores comments, so they are not executed.

Types of Comments:
There are two types of comments in JavaScript: single-line comments and multi-line
comments.
1] Single-line comments:
- In JavaScript, use double slashes (//) to write a single-line comment.
- Single-line comments start with // and continue until the end of the line.
- You can use single-line comments on multiple lines by adding // at the beginning of
each line.
- Example
<html><body>
<script language="javascript" type="text/javascript">
// This is a JavaScript program that displays a message.
// This program was developed by MahindraTech Company.
document.write("Welcome to the world of JavaScript");
</script>
</body></html>
2] Multi-line comments:
- In JavaScript, multi-line comments start with /* and end with */.
- They can cover across multiple lines.
- Multi-line comments are useful for providing longer explanations.
- Example
<html><body>
<script language="javascript" type="text/javascript">
// This is a JavaScript program that displays a message.
// This program was developed by MahindraTech Company.
document.write("Welcome to the world of JavaScript");
</script>
</body></html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 8


Keywords in JavaScript:
• Keywords are reserved words in JavaScript.
• Each and Every keyword has some special meaning.
• So they can’t be used as variable name, function name or for some other purpose.

Operators:
- Operator is a symbol which indicates operation to be performed.
- Operands are the variable on which operation to be performed.
- The systematic arrangement of operators and operands is known as Expression.

There are following types of operators in JavaScript.


1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Conditional Operators

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 9


1] Arithmetic Operators:
- Arithmetic operators are used to perform mathematical operations on the
operands.
- The following operators are known as JavaScript arithmetic operators.

Example

<html> <body>

<script language="javascript" type="text/javascript">

var a = 5;

var b = 3;

document.write("<br>Addition: " + (a + b)); //8

document.write("<br>Subtraction: " + (a - b)); //2

document.write("<br>Multiplication: " + (a * b)); //15

document.write("<br>Division: " + (a / b)); //1.66

document.write("<br>Remainder: " + (a % b)); //2

document.write("<br>Increment: " + (++a)); //6

document.write("<br>Decrement: " + (--b)); //2

</script>

</body></html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 10


2] Comparison Operators:

- The JavaScript comparison operator compares the two operands.


- It will give the result as true or false according to the condition.
- The comparison operators are as follows:

Example

<html>
<body>
<script language="javascript" type="text/javascript">
var a = 5;
var b = 3;
document.write("<br>Equal to (==): " + (a == b)); // false
document.write("<br>Not equal to (!=): " + (a != b)); // true
document.write("<br>Less than (<): " + (a < b)); // false
document.write("<br>Greater than (>): " + (a > b)); // true
document.write("<br>Less than or equal to (<=): " + (a <= b)); // false
document.write("<br>Greater than or equal to (>=): " + (a >= b)); // true
document.write("<br>Strict equal to (===): " + (a === b)); // false
document.write("<br>Strict not equal to (!==): " + (a !== b)); // true
</script>
</body>
</html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 11


3] Bitwise Operators:
- The bitwise operators perform bitwise operations on operands.
- The bitwise operator works on bits and performs bit by bit operation.
- The bitwise operators are as follows:

Operator Meaning Description


& Bitwise AND If both bits are 1 then output is 1 otherwise 0.
| Bitwise OR If both bits are 0 then output is 0 otherwise 1.
^ Bitwise XOR If both bits are different then output is 1 otherwise 0
~ Bitwise NOT Adds +1 to the number and change the sign.
<< Bitwise left Shift Shift left by pushing zeros in from the right side and let
the leftmost bits fall off
>> Bitwise right shift Shift right by pushing zeros in from the left side and let
the rightmost bits fall off

Example

<html>

<body>

<script language="javascript" type="text/javascript">

var a = 5;

var b = 3;

document.write("<br>Bitwise AND (&): " + (a & b)); // 1

document.write("<br>Bitwise OR (|): " + (a | b)); // 7

document.write("<br>Bitwise XOR (^): " + (a ^ b)); // 6

document.write("<br>Bitwise NOT (~): " + (~a)); // -6

document.write("<br>Left Shift (<<): " + (a << b)); // 40

document.write("<br>Right Shift (>>): " + (a >> b)); // 0

</script>

</body>

</html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 12


4] Logical Operators:

- The following operators are known as JavaScript logical operators.


- Logical operators are the &&, ||, ! operators.
- It give result either true or false according to the condition.

Operator Meaning
&& It returns true if its both sides are true otherwise false
|| It returns false if its both sides are false otherwise true
! Reverse the Result, returns false if the result is true.

- Example:

<html>
<body>
<script language="javascript" type="text/javascript">
var a = true;
var b = false;
document.write("<br>Logical AND (&&): " + (a && b)); // false
document.write("<br>Logical OR (||): " + (a || b)); // true
document.write("<br>Logical NOT (!): " + !a); // false
</script>
</body>
</html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 13


5] Assignment Operators:

- Assignment operator is used to assign right side expression result or value to the
left side variable.
- Assignment operators are used in JavaScript to assign values to variables.
- The following operators are known as JavaScript assignment operators.

Example:

<html>

<body>

<script language="javascript" type="text/javascript">

var a = 5;

var b = 3;

document.write("<br>Assignment (=): a = " + a); // a = 5

document.write("<br>Addition and assignment (+=): a = " + (a += b)); // a = 8

document.write("<br>Subtraction and assignment (-=): a = " + (a -= b)); // a = 5

document.write("<br>Multiplication and assignment (*=): a = " + (a *= b)); // a = 15

document.write("<br>Division and assignment (/=): a = " + (a /= b)); // a = 5

document.write("<br>Modulus and assignment (%=): a = " + (a %= b)); // a = 2

</script>

</body>

</html

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 14


6] Conditional Operator:

- The operator (?:) is known as conditional operator.


- It is also called as Ternary operator.
- Syntax:
Condition? Expression-1 : Expression-2;

- In above syntax, if condition is true then it will execute Expression-1 otherwise


Expression-2.

Example:

<html>

<body>

<script type = "text/javascript">

var a = 100;
var b = 20;
var result;

result = (a > b) ? a : b;
document.write("Greatest Number="+result); //100
</script>

</body>

</html>

7] Increment / Decrement Operators:

- Increment operator (++) adds +1 to the current value.


- Decrement operator (--) subtracts -1 to the current value.
- Syntax:
variableName [++][--]
OR
[++] [--] variableName

Example:

var a =10;
document.write(a++); //11
document.write(a--); //9

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 15


JavaScript Expressions:
• JavaScript expression is a valid set of literals, operators & expressions that evaluate
to a single value.
• JavaScript expressions are combinations of values, variables, and operators that
produce new values.
• They are like building blocks that allow us to perform different actions in our code.

Types / Categories of Expression

1] Primary Expression:

- These are the simplest expressions and include basic values like numbers, strings,
and boolean values.
- For example, 5, "Hello", and true are primary expressions.
- Number Literal – 3.2, String literal – “Welcome”, Regular Exp Literal - / pattern /
2] Array and Object Initializers Expressions:

- These are the expressions whose values are created by object or array. These are
also known as “Object Literals” or “Array Literals”.
- These expressions allow us to create arrays and objects.
- We can define a list of values enclosed in square brackets [ ] for arrays and key-
value pairs enclosed in curly braces { } for objects.
- For example, [1, 2, 3] is an array initializer expression, and { name: "Laxmikant", age:
18 } is an object initializer expression.
3] Function Defination Expressions:

- It is a function literal to define function in JavaScript. Value of those expression is


newly created function.
- function keyword is used to define a function with a list of zero or more parameters,
separate by comma in the parantheses with JS code in curly brackets.
- For example, function(a, b) { return a + b; } is a function definition expression.
4] Property Access Expressions:

- These expressions allow us to access properties or values of objects.


- We use dot notation (object.property) or square brackets (object["property"]) to
access the desired property.
- For example, person.name or person["age"] are property access expressions.
5] Invocation Expressions:

- These expressions involve calling a function and passing arguments to it.


- We use parentheses after the function name and provide the arguments inside the
parentheses.
- For example, add(3, 5) is an invocation expression that calls the add function with
arguments 3 and 5.

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 16


Decision Making Statements:
• Decision making statements decide the order of execution of statements based on
certain conditions.
• They only execute when the condition is true else they skip the execution of their
body.
Decision Making Statements are:
1] If Statement
2] If else Statement
3] Else if ladder
4] Nested if statement
5] Switch Case Statement

1] if Statement
- It is a simple decision making statement.
- It is used to test a single condition.
- If the condition is true, then it will execute the body of if statement
- If the condition is false, then it will skip the execution of body of if statement.
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var age = 20;
if (age >= 18) {
document.write("You are eligible for voting"); // Outputs if the condition is true
}
</script>
</body>
</html>

Output : You are eligible for voting

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 17


2] if else Statement
- if else helps us test a condition and execute different blocks of code based on the
result.
- If the condition is true, the code inside the if block will be executed.
- If the condition is false, the code inside the else block (if present) will be executed, or
the program will continue to the next statement.
Syntax:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var age = 20;
if (age >= 18) {
document.write("You are eligible for voting"); // Outputs if the condition is true
} else {
document.write("You are not eligible for voting"); // Outputs if the condition is false
}
</script>
</body>
</html>

Output: You are eligible for voting

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 18


3] else if ladder (if else .. if)
- The "else if" ladder is used when we have multiple conditions to check, and we want
to execute different code blocks based on those conditions.
- It is an extension of the if-else statement and allows us to test multiple conditions one
by one.
- Each condition is checked in order, and if a condition is true, the corresponding code
block is executed. If none of the conditions are true, the code inside the else block (if
present) is executed.
Syntax:
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else if (condition3) {
// code to execute if condition3 is true
} else {
// code to execute if all conditions are false
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var num = 10;
if (num > 0) {
document.write("The number is positive.");
} else if (num < 0) {
document.write("The number is negative.");
} else {
document.write("The number is zero.");
}
</script>
</body></html>
Output: The number is positive.

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 19


4] Nested if else Statement
- An if statement inside another if statement is known as nested if statement.
- It allows us to test for more complex conditions within a condition
Syntax:
if (condition1) {
// code block executed if condition1 is true

if (condition2) {
// code block executed if condition2 is true
} else {
// code block executed if condition2 is false
}
} else {
// code block executed if condition1 is false
}
Example:
<html><body>
<script language="javascript" type="text/javascript">
var no=5;
if(no!=0){
if(no>0){
document.write("Positive Number");
}
else{
document.write("Negative Number")
}
}
else{
document.write("Zero Number")
}
</script></body></html> Output: Positive Number

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 20


5] Switch case Statement
- It is a selection statement.
- Here Expression value is matched with the case value and if match, then
corresponding block of code will get execute.
- Keywords -> switch, case, break, default
- break statement is used to terminate the switch case.
- If none case matched, then default block will get execute.
Syntax:
switch (expression) {
case value-1: //block of code
break;
case value-2: //block of code
break;
…..
case value-N: //block of code
break;
default: //block of code
}
Example:
<html><body>

<script language="javascript" type="text/javascript">

var no = parseInt(prompt("Enter a Number: "));

switch (no) {

case 1:

document.write("1");

break;

case 2:

document.write("2");

break;

case 3: document.write("3");

break;

default:

document.write("Invalid Input");

} </script> </body></html>

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 21


Looping Statements:
• Loops are used to execute a block of code multiple times.
• Loops are used to do the same task that is repeated multiple times.
Looping Statements are:
1] For Loop
2] While Loop
3] Do-while Loop
4] For-in Loop

1] For Loop
- For Loop is a entry controlled loop.
- It executes the block of code specified number of times.
- If we know the number of iterations to be executed, then we can use the for loop.
- Program controller first goes to initialization section, then checks the condition, if
condition is true, then it will execute the body of loop, then it goes to
increment/decrement section, then again checks the condition. And this process is
repeated till condition becomes false.
Syntax:
for (initialization; condition; increment/decrement) {
//body of for loop
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
for(var i=0; i<=5; i++){
document.write(i+" ");
}
</script>
</body>
</html>

Output : 0 1 2 3 4 5

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 22


2] While Loop
- While Loop is a entry controlled loop.
- It executes the block of code until condition is true.
- Program controller firstly checks the condition, if condition is true, then only it will
execute the body of while loop, if the condition is false, then it will skip the execution
of body of while loop.
Syntax:
while (condition) {
//body of while loop
}

Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var i=0;
while(i<=5){
document.write(i+" ");
i++;
}
</script>
</body>
</html>

Output : 0 1 2 3 4 5

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 23


3] Do- While Loop
- Do-While Loop is a exit controlled loop.
- Do-while Loop is similar to while loop but executes the block of code atleast once
even if condition is false.
- Program controller firstly executes the body of do while, then check the condition, if
condition is satisfied, then only it will execute again body of do while else not.
Syntax:
do {
//body of do-while loop
}while(condition);
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var i=0;
do {
document.write(i+" ");
i++;
} while(i<=5);
</script>
</body>
</html>

Output : 0 1 2 3 4 5

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 24


4] For-in Loop
- The for...in loop is used to loop through an object's properties.
- It loops through a block of code for each property in the object.
- In each iteration, one property from object is assigned to variablename and this loop
continues till all the properties of the object are exhausted.
Syntax:
for (variablename in object)
{
//block of code
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var aProperty;
document.write("Navigator Object Properties<br> ");

for (aProperty in navigator) {


document.write(aProperty);
document.write("<br>");
}
</script>
</body>
</html>

Output : It will display all the properties of the navigator.

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 25


Loop Control Statement:
• Loop control statements in JavaScript are used to control the flow of execution within
loops.
Looping Statements are:
1] Break statement
2] Continue statement

1] Break Statement
- The break statement is used to transfer the control to the end of the loop.
- When break statement is applied then loop gets terminate and control goes to the
next line pointing after the loop body.
- It is generally used with decision making statement such as if-else.
- It is used to terminate a loop.
Syntax:
If(condition){
break;
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
for(var i=0; i<=5; i++){
if(i==3){
break;
}
document.write(i+" ");
}
</script>
</body>
</html>

Output : 0 1 2

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 26


2] Continue Statement
- The continue statement is used to skip some statements inside the loop.
- It is used with decision making statement such as if else.
- When continue statement is applied then program controller skips the current
iteration of the loop and goes to the starting of the loop.
- It does not terminate the loop.
Syntax:
If(condition){
continue;
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
for(var i=0; i<=5; i++){
if(i==3){
continue;
}
document.write(i+" ");
}
</script>
</body>
</html>

Output : 0 1 2 4 5

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 27


Object:
An object in JavaScript is a collection of key-value pairs, where each key represents
a property and its associated value. It is a way to group related data and functionality
together.
We can create an object using object literal and define its properties using curly
braces { }.
Example:
var person = {
name: "John",
age: 25,
gender: "Male"
};

Here, we have created a person object with the properties as name, age, gender.

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 28


Querying and Setting Properties:

Querying Properties:
To retrieve the value of a property from an object, you can use dot notation or square
bracket notation.
1] Dot Notation: Use the dot operator after the object name to access the property value
directly.
Example: var person = { name: 'Laxmikant', age: 18 };
document.write(person.name); // Output: Laxmikant

2] Square Bracket Notation: Enclose the property name in square brackets after the
object name to access the property value.
Example: var person = { name: 'Laxmikant', age: 18 };
document.write(person['age']); // Output: 18

Setting Properties:
To assign a new value to a property or create a new property in an object, you can
use assignment with dot or square bracket notation.
1] Dot Notation: Use the dot operator to assign a new value to an existing property or
create a new property in the object.
Example: var person = { name: 'Laxmikant', age: 18 };
person.age = 30; // Modifying existing property

2] Square Bracket Notation: Use square brackets with the property name as a string to
assign a new value or create a new property.
Example: var person = { name: 'Laxmikant', age: 18 };
person['gender'] = 'Male'; // Adding new property

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 29


Setter and Getter Property
In JavaScript, accessor properties are methods that get or set the value of an
object.
For that we use two keywords.
I] get – to define a getter method, to get the property value.
II] set – to define a setter method, to set the property value.

1] Getter Method:
In JavaScript, getter methods are used to access the properties of an object.
2] Setter Method:
Setter methods are used to change the value of object.
Example:
<html> <body>
<script language="javascript" type="text/javascript">
var student = {
name: "Laxmikant",
age: 18,
get getName() {
return this.name;
},
set setName(nm) {
this.name = nm;
}
};

document.write("Name: " + student.getName); //Laxmikant


student.setName = "Lucky";
document.write("<br>New name: " + student.getName); //Lucky
</script>
</body> </html>
✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 30
Delete Property:
• Delete is comparatively lesser-known operator in JavaScript.
• This operator is used to delete the object properties.
• The JavaScript pop() and shift() methods are available to delete an element from
an array.
• But, because of key-value pair in an object, the deleting is more complicated.
• The delete operator only works on objects and not on variables or functions.
• This operator returns true if it removes a property.

Example:

<html>
<body>
<script language="javascript" type="text/javascript">
var student = {
name: "Laxmikant",
id: 352,
rollno: 5
};
document.write(delete student.id);
</script>
</body>
</html>

Output -> true

✓ BY LAXMIKANT MAHINDRAKAR (MAHINDRATECH COACHING) - +91 8767369115 31

You might also like