SlideShare ist ein Scribd-Unternehmen logo
1 von 10
JAVASCRIPT
       COMPARISON AND
     LOGICAL OPERATORS
BENJAMIN B. ESGUERRA MEMORIAL NATIONAL HIGH
                  SCHOOL
JavaScript Comparison and Logical
Operators

  Comparison and Logical operators are
   used to test for true or false.
  Comparison Operators are used in
   logical statements to determine equality
   or difference between variables or values.
  Logical Operators are used to determine
   the logic between variables or values.
COMPARISON OPERATORS
 Given that x=5, the table below explains the comparison operators.

Operator                 Description                 Comparing   Returns
                                                  x==8            FALSE
   ==      is equal to                            x==5            TRUE
                                                  x==="5"         FALSE
           is exactly equal to (value and type)   x===5           TRUE
   !=      is not equal                           x!=8            TRUE
                                                  x!=="5"         TRUE
  !==      is not equal (neither value or type)   x!==5           FALSE
   >       is greater than                        x>8             FALSE
   <       is less than                           x<8             TRUE
  >=       is greater than or equal to            x>=8            FALSE
  <=       is less than or equal to               x<=8            TRUE
EXAMPLE
<html>
<body>
<script>
var x= 10
var y= 2
var z= (x>y);
confirm("Is "+x+" greater
than "+y+" ?");
alert(""+z+"");
</script>
</body>
</html>
EXAMPLE
<html>
<body>
<script>
var x= parseFloat(prompt(“Enter
  the first value”,””));
var y= parseFloat(prompt(“Enter
  the second value”,””));
confirm(“Is”+x+”
var z= (x>y);
alert(""+z+"");
</script>
</body>
</html>
LOGICAL OPERATORS
Given that x=6 and y=3 the table below explains the logical
operators.



Operator       Description          Comparing            Returns
    &&
(ampersand)         and            (x<10 && y>1)              TRUE

     ||              or             (x==5||y==5)              FALSE

     !              not                !(x==y)                TRUE
<script>
var x= 10
var y= 2
var z= (x>y && y<x)
confirm("Is "+x+" greater than "+y+" ?, and
  "+y+" is less than "+x+" ?");
alert(""+z+"");
</script>
</body>
</html>
CONDITIONAL OPERATOR
 JavaScript also contains a conditional operator that assigns a
 value to a variable based on some conditions.

SYNTAX:

variable name= (condition)? value1:value2

Example:
If the variable age is value below 18, the value of the
   variable voteable will be “Too young, otherwise the
   value of votaable will be “Old enough”

var age= 18
var voteable= (age<18)? “Too young”:”Old
 enough”
<html>
<body>
<script>
var x= prompt("Who created the JavaScript? "," ");
var y= “Brendan Eich"
var z= (x==y)?"Correct": "Wrong";
alert(""+z+"");
document.write("Correct Answer: "+y+"");
</script>
</body>
</html>
Assign/Seatwork
Create the JavaScript source code

COMPARISON
50 is less than 68

LOGICAL
50 is equal to 68 and 68 is equal to 50

CONDITIONAL
What is the first calculating device? Abacus
  or Comptometer?
Condition: Congratulation! And Try Again

Weitere ähnliche Inhalte

Was ist angesagt?

Strings in Java
Strings in Java Strings in Java
Strings in Java Hitesh-Java
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)Sujit Majety
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaEdureka!
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm Madishetty Prathibha
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript ObjectsReem Alattas
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java ProgrammingMath-Circle
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and FunctionsJussi Pohjolainen
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptAndres Baravalle
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 

Was ist angesagt? (20)

Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Operators in java
Operators in javaOperators in java
Operators in java
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 

Andere mochten auch

Css border and outline properties
Css border and outline propertiesCss border and outline properties
Css border and outline propertiesJesus Obenita Jr.
 
Designing web pages html forms and input
Designing web pages html  forms and inputDesigning web pages html  forms and input
Designing web pages html forms and inputJesus Obenita Jr.
 
Web browser, linux, bit & bytes, files
Web browser, linux, bit & bytes, filesWeb browser, linux, bit & bytes, files
Web browser, linux, bit & bytes, filesJesus Obenita Jr.
 
Designing web page marquee and img tag
Designing web page  marquee and img tagDesigning web page  marquee and img tag
Designing web page marquee and img tagJesus Obenita Jr.
 

Andere mochten auch (8)

Css border and outline properties
Css border and outline propertiesCss border and outline properties
Css border and outline properties
 
Css external style sheet
Css external style sheetCss external style sheet
Css external style sheet
 
Id and class selector
Id and class selectorId and class selector
Id and class selector
 
Designing web pages html forms and input
Designing web pages html  forms and inputDesigning web pages html  forms and input
Designing web pages html forms and input
 
Web browser, linux, bit & bytes, files
Web browser, linux, bit & bytes, filesWeb browser, linux, bit & bytes, files
Web browser, linux, bit & bytes, files
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
 
Picasa&gimp v2.0
Picasa&gimp v2.0Picasa&gimp v2.0
Picasa&gimp v2.0
 
Designing web page marquee and img tag
Designing web page  marquee and img tagDesigning web page  marquee and img tag
Designing web page marquee and img tag
 

Ähnlich wie JavaScript Comparison and Logical Operators Explained

Ähnlich wie JavaScript Comparison and Logical Operators Explained (20)

Php
PhpPhp
Php
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Java scripts
Java scriptsJava scripts
Java scripts
 
Javascript conditional statements 1
Javascript conditional statements 1Javascript conditional statements 1
Javascript conditional statements 1
 
Java script questions
Java script questionsJava script questions
Java script questions
 
Php + my sql
Php + my sqlPhp + my sql
Php + my sql
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Wrong
WrongWrong
Wrong
 
Ref Lec 4- Conditional Statement (1).pptx
Ref Lec 4- Conditional Statement (1).pptxRef Lec 4- Conditional Statement (1).pptx
Ref Lec 4- Conditional Statement (1).pptx
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In R
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
python operators.pptx
python operators.pptxpython operators.pptx
python operators.pptx
 
Python
PythonPython
Python
 
3- Operators in Java
3- Operators in Java3- Operators in Java
3- Operators in Java
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Chapter 4.2
Chapter 4.2Chapter 4.2
Chapter 4.2
 

Mehr von Jesus Obenita Jr.

Mehr von Jesus Obenita Jr. (20)

Organization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management TheoryOrganization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management Theory
 
Organization and management 2 Management Function
Organization and management 2 Management FunctionOrganization and management 2 Management Function
Organization and management 2 Management Function
 
Organization and management 1
Organization and management 1Organization and management 1
Organization and management 1
 
Ms excel 2013 formatting worksheets
Ms excel 2013 formatting worksheetsMs excel 2013 formatting worksheets
Ms excel 2013 formatting worksheets
 
Ms excel 2013 data management
Ms excel 2013 data managementMs excel 2013 data management
Ms excel 2013 data management
 
Microsoft Excel introduction
Microsoft Excel introductionMicrosoft Excel introduction
Microsoft Excel introduction
 
Word 2013 working with pictures
Word 2013 working with picturesWord 2013 working with pictures
Word 2013 working with pictures
 
Word 2013 Formatting Page
Word 2013 Formatting PageWord 2013 Formatting Page
Word 2013 Formatting Page
 
Word 2013 8
Word 2013 8Word 2013 8
Word 2013 8
 
Ms word 2013 7
Ms word 2013 7Ms word 2013 7
Ms word 2013 7
 
Ms word 2013 6
Ms word 2013 6Ms word 2013 6
Ms word 2013 6
 
Ms word 2013 4
Ms word 2013 4Ms word 2013 4
Ms word 2013 4
 
Ms word 2013 2
Ms word 2013 2Ms word 2013 2
Ms word 2013 2
 
Ms word 2013
Ms word 2013Ms word 2013
Ms word 2013
 
Parts of the ms word 2013 screen and
Parts of the ms word 2013 screen andParts of the ms word 2013 screen and
Parts of the ms word 2013 screen and
 
Word processor
Word processorWord processor
Word processor
 
Session 2 test construction.mt's
Session 2   test construction.mt'sSession 2   test construction.mt's
Session 2 test construction.mt's
 
Cooking ingredients
Cooking ingredientsCooking ingredients
Cooking ingredients
 
Color theory
Color theoryColor theory
Color theory
 
Computer software
Computer softwareComputer software
Computer software
 

JavaScript Comparison and Logical Operators Explained

  • 1. JAVASCRIPT COMPARISON AND LOGICAL OPERATORS BENJAMIN B. ESGUERRA MEMORIAL NATIONAL HIGH SCHOOL
  • 2. JavaScript Comparison and Logical Operators  Comparison and Logical operators are used to test for true or false.  Comparison Operators are used in logical statements to determine equality or difference between variables or values.  Logical Operators are used to determine the logic between variables or values.
  • 3. COMPARISON OPERATORS Given that x=5, the table below explains the comparison operators. Operator Description Comparing Returns x==8 FALSE == is equal to x==5 TRUE x==="5" FALSE is exactly equal to (value and type) x===5 TRUE != is not equal x!=8 TRUE x!=="5" TRUE !== is not equal (neither value or type) x!==5 FALSE > is greater than x>8 FALSE < is less than x<8 TRUE >= is greater than or equal to x>=8 FALSE <= is less than or equal to x<=8 TRUE
  • 4. EXAMPLE <html> <body> <script> var x= 10 var y= 2 var z= (x>y); confirm("Is "+x+" greater than "+y+" ?"); alert(""+z+""); </script> </body> </html>
  • 5. EXAMPLE <html> <body> <script> var x= parseFloat(prompt(“Enter the first value”,””)); var y= parseFloat(prompt(“Enter the second value”,””)); confirm(“Is”+x+” var z= (x>y); alert(""+z+""); </script> </body> </html>
  • 6. LOGICAL OPERATORS Given that x=6 and y=3 the table below explains the logical operators. Operator Description Comparing Returns && (ampersand) and (x<10 && y>1) TRUE || or (x==5||y==5) FALSE ! not !(x==y) TRUE
  • 7. <script> var x= 10 var y= 2 var z= (x>y && y<x) confirm("Is "+x+" greater than "+y+" ?, and "+y+" is less than "+x+" ?"); alert(""+z+""); </script> </body> </html>
  • 8. CONDITIONAL OPERATOR JavaScript also contains a conditional operator that assigns a value to a variable based on some conditions. SYNTAX: variable name= (condition)? value1:value2 Example: If the variable age is value below 18, the value of the variable voteable will be “Too young, otherwise the value of votaable will be “Old enough” var age= 18 var voteable= (age<18)? “Too young”:”Old enough”
  • 9. <html> <body> <script> var x= prompt("Who created the JavaScript? "," "); var y= “Brendan Eich" var z= (x==y)?"Correct": "Wrong"; alert(""+z+""); document.write("Correct Answer: "+y+""); </script> </body> </html>
  • 10. Assign/Seatwork Create the JavaScript source code COMPARISON 50 is less than 68 LOGICAL 50 is equal to 68 and 68 is equal to 50 CONDITIONAL What is the first calculating device? Abacus or Comptometer? Condition: Congratulation! And Try Again