Java Script Operators
DR.G.JASMINE BEULAH
ASSISTANT PROFESSOR, KRISTU JAYANTI COLLEGE, BENGALURU
Assign values to variables and add them together:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>x = 5, y = 2, calculate z = x + y, and display z:</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
The assignment operator (=) assigns a value to
a variable.
<!DOCTYPE html>
<html>
<body>
<h2>The = Operator</h2>
<p id="demo"></p>
<script>
var x = 10;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
JavaScript Arithmetic Operators
JavaScript Assignment Operators
Assignment
<!DOCTYPE html>
<html>
<body>
<h2>The += Operator</h2>
<p id="demo"></p>
<script>
var x = 10;
x += 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
JavaScript String Operators
The + operator can also be used to add
(concatenate) strings.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>The + operator concatenates (adds) strings.</p>
<p id="demo"></p>
<script>
var txt1 = "John";
var txt2 = "Doe";
document.getElementById("demo").innerHTML = txt1 + " " + txt2;
</script>
</body>
</html>
Adding Strings and Numbers
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>Adding a number and a string, returns a string.</p>
<p id="demo"></p>
<script>
var x = 5 + 5;
var y = "5" + 5;
var z = "Hello" + 5;
document.getElementById("demo").innerHTML =
x + "<br>" + y + "<br>" + z;
</script>
</body>
</html>
JavaScript Comparison Operators
JavaScript Logical Operators
JavaScript Type Operators
JavaScript if else and else if
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
The 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
}
The If Statement
<html>
<body>
<p>Display "Good day!" if the hour is less than 18:00:</p>
<p id="demo">Good Evening!</p>
<script>
if (new Date().getHours() < 18)
{
document.getElementById("demo").innerHTML = "Good day!";
}
</script>
</body>
</html>
<html>
<body>
<p>Click the button to display a time-based greeting:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var hour = new Date().getHours();
var greeting;
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
document.getElementById("demo").innerHTML = greeting;
}
</script>
</body>
</html>
The JavaScript Switch Statement
 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
}
This is how it works:
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 If there is no match, the default code block is executed.
<html>
<body>
<p id="demo"></p>
<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;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>
</body>
</html>

More Related Content

PDF
Automated testing for client-side - Adam Klein, 500 Tech
PPTX
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
PPTX
Behavior driven development with OPA5 automated tests
PPTX
Operators
PPTX
Learning Svelte
PDF
Asp.Net MVC Framework Design Pattern
PDF
Introduction to AngularJS
PDF
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View
Automated testing for client-side - Adam Klein, 500 Tech
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
Behavior driven development with OPA5 automated tests
Operators
Learning Svelte
Asp.Net MVC Framework Design Pattern
Introduction to AngularJS
Odoo Experience 2018 - Visualizing Data in Odoo: How to Create a New View

What's hot (20)

PPTX
Java script events
PDF
Backbone.js
PDF
Testing in JavaScript - August 2018 - WebElement Bardejov
PDF
Accessible Ajax on Rails
PPT
Activity lesson
PDF
Human Talks - StencilJS
PDF
Modular Angular JS
PDF
Сергей Больщиков "Protractor Tips & Tricks"
DOCX
PPTX
Ch2(working with forms)
PPTX
KEY
Making Django and NoSQL Play Nice
PPTX
How Reactive do we need to be
PDF
Svelte JS introduction
PDF
Writing testable js [by Ted Piotrowski]
PDF
Ajax
PPTX
Java script functions
PPTX
Introduction to react and redux
PDF
Introduction to Protractor
PDF
2013-06-25 - HTML5 & JavaScript Security
Java script events
Backbone.js
Testing in JavaScript - August 2018 - WebElement Bardejov
Accessible Ajax on Rails
Activity lesson
Human Talks - StencilJS
Modular Angular JS
Сергей Больщиков "Protractor Tips & Tricks"
Ch2(working with forms)
Making Django and NoSQL Play Nice
How Reactive do we need to be
Svelte JS introduction
Writing testable js [by Ted Piotrowski]
Ajax
Java script functions
Introduction to react and redux
Introduction to Protractor
2013-06-25 - HTML5 & JavaScript Security
Ad

Similar to JavaScript Operators (20)

PPTX
Introduction to java script
PPTX
csc ppt 15.pptx
RTF
Java scripts
PPTX
Javascript
PPSX
DIWE - Programming with JavaScript
PPTX
Java script ppt from students in internet technology
PPTX
Java script.pptx v
PPT
Introduction to javascript.ppt
PPTX
JAVASCRIPT - LinkedIn
PPTX
Unit - 4 all script are here Javascript.pptx
PPTX
javascriptbasicsPresentationsforDevelopers
PDF
Javascript basics
PPT
UNIT 3.ppt
PDF
JavaScript Notes 🔥.pdfssssssssssssssssssssssssssssssssssssssssss
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
PDF
Ch3- Java Script.pdf
PPT
data-types-operators-datatypes-operators.ppt
PDF
JavaScript for beginners
DOC
Introduction to java script
PPTX
wp-UNIT_III.pptx
Introduction to java script
csc ppt 15.pptx
Java scripts
Javascript
DIWE - Programming with JavaScript
Java script ppt from students in internet technology
Java script.pptx v
Introduction to javascript.ppt
JAVASCRIPT - LinkedIn
Unit - 4 all script are here Javascript.pptx
javascriptbasicsPresentationsforDevelopers
Javascript basics
UNIT 3.ppt
JavaScript Notes 🔥.pdfssssssssssssssssssssssssssssssssssssssssss
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
Ch3- Java Script.pdf
data-types-operators-datatypes-operators.ppt
JavaScript for beginners
Introduction to java script
wp-UNIT_III.pptx
Ad

More from Dr. Jasmine Beulah Gnanadurai (20)

PPTX
Chapter 4 Requirements Engineering2.pptx
PPTX
Chapter 4 Requirement Engineering1 .pptx
PPTX
Chapter 2 Software Processes Processes.pptx
PPT
Programming in Python Lists and its methods .ppt
PPT
Introduction to UML, class diagrams, sequence diagrams
PPT
Software Process Models in Software Engineering
PPT
ch03-Data Modeling Using the Entity-Relationship (ER) Model.ppt
PPT
Process Model in Software Engineering Concepts
PPTX
Arrays and Detailed explanation of Array
PPTX
Data Warehouse_Architecture.pptx
PPTX
DMQL(Data Mining Query Language).pptx
PPTX
PPTX
KBS Architecture.pptx
PPTX
Knowledge Representation in AI.pptx
PPTX
File allocation methods (1)
PPTX
Segmentation in operating systems
PPTX
Association rules apriori algorithm
Chapter 4 Requirements Engineering2.pptx
Chapter 4 Requirement Engineering1 .pptx
Chapter 2 Software Processes Processes.pptx
Programming in Python Lists and its methods .ppt
Introduction to UML, class diagrams, sequence diagrams
Software Process Models in Software Engineering
ch03-Data Modeling Using the Entity-Relationship (ER) Model.ppt
Process Model in Software Engineering Concepts
Arrays and Detailed explanation of Array
Data Warehouse_Architecture.pptx
DMQL(Data Mining Query Language).pptx
KBS Architecture.pptx
Knowledge Representation in AI.pptx
File allocation methods (1)
Segmentation in operating systems
Association rules apriori algorithm

Recently uploaded (20)

PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
DOCX
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
PDF
Unleashing the Potential of the Cultural and creative industries
PPTX
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
PDF
anganwadi services for the b.sc nursing and GNM
PPTX
climate change of delhi impacts on climate and there effects
PPTX
Neurological complocations of systemic disease
PPTX
GW4 BioMed Candidate Support Webinar 2025
PPTX
Neurology of Systemic disease all systems
PDF
Health aspects of bilberry: A review on its general benefits
PPTX
Copy of ARAL Program Primer_071725(1).pptx
PDF
Physical pharmaceutics two in b pharmacy
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PPTX
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
THEORY AND PRACTICE ASSIGNMENT SEMESTER MAY 2025.docx
Unleashing the Potential of the Cultural and creative industries
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
WHAT NURSES SAY_ COMMUNICATION BEHAVIORS ASSOCIATED WITH THE COMP.pdf
anganwadi services for the b.sc nursing and GNM
climate change of delhi impacts on climate and there effects
Neurological complocations of systemic disease
GW4 BioMed Candidate Support Webinar 2025
Neurology of Systemic disease all systems
Health aspects of bilberry: A review on its general benefits
Copy of ARAL Program Primer_071725(1).pptx
Physical pharmaceutics two in b pharmacy
Theoretical for class.pptxgshdhddhdhdhgd
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
pharmaceutics-1unit-1-221214121936-550b56aa.pptx

JavaScript Operators

  • 1. Java Script Operators DR.G.JASMINE BEULAH ASSISTANT PROFESSOR, KRISTU JAYANTI COLLEGE, BENGALURU
  • 2. Assign values to variables and add them together: <!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>x = 5, y = 2, calculate z = x + y, and display z:</p> <p id="demo"></p> <script> var x = 5; var y = 2; var z = x + y; document.getElementById("demo").innerHTML = z; </script> </body> </html>
  • 3. The assignment operator (=) assigns a value to a variable. <!DOCTYPE html> <html> <body> <h2>The = Operator</h2> <p id="demo"></p> <script> var x = 10; document.getElementById("demo").innerHTML = x; </script> </body> </html>
  • 6. Assignment <!DOCTYPE html> <html> <body> <h2>The += Operator</h2> <p id="demo"></p> <script> var x = 10; x += 5; document.getElementById("demo").innerHTML = x; </script> </body> </html>
  • 7. JavaScript String Operators The + operator can also be used to add (concatenate) strings. <!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>The + operator concatenates (adds) strings.</p> <p id="demo"></p> <script> var txt1 = "John"; var txt2 = "Doe"; document.getElementById("demo").innerHTML = txt1 + " " + txt2; </script> </body> </html>
  • 8. Adding Strings and Numbers <!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>Adding a number and a string, returns a string.</p> <p id="demo"></p> <script> var x = 5 + 5; var y = "5" + 5; var z = "Hello" + 5; document.getElementById("demo").innerHTML = x + "<br>" + y + "<br>" + z; </script> </body> </html>
  • 12. JavaScript if else and else if 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
  • 13. The 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 }
  • 14. The If Statement <html> <body> <p>Display "Good day!" if the hour is less than 18:00:</p> <p id="demo">Good Evening!</p> <script> if (new Date().getHours() < 18) { document.getElementById("demo").innerHTML = "Good day!"; } </script> </body> </html>
  • 15. <html> <body> <p>Click the button to display a time-based greeting:</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var hour = new Date().getHours(); var greeting; if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; } </script> </body> </html>
  • 16. The JavaScript Switch Statement  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 } This is how it works:  The switch expression is evaluated once.  The value of the expression is compared with the values of each case.  If there is a match, the associated block of code is executed.  If there is no match, the default code block is executed.
  • 17. <html> <body> <p id="demo"></p> <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;
  • 18. case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; } document.getElementById("demo").innerHTML = "Today is " + day; </script> </body> </html>