Quantcast
Channel: สอน PHP สอนทำเว็บด้วย Joomla ระบบ CRM บทความ Hosting - สอน PHP สอนทำเว็บด้วย Joomla ระบบ CRM บทความ Hosting
Viewing all articles
Browse latest Browse all 24

Chapter 11 - JavaScript Comparison and Logical Operators

$
0
0

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.

Given that x=5, the table below explains the comparison operators:

Operators                        Description                  Comparing Returns
== equal to x==8 false
     x==5  true
  === exactly equal to x==="5" false
   (equal to value and equal type) x===5 true
 !=  not equal  x!8 true
 !== not equal (different values   x!=="5" true
   or different type)  x!==5  false
 > greater than  x>8 false 
 < less than   x<8 true
 >= greater than or equal to x>=8  false 
 <= less than or equal to   x<=8 true

 

Logical Operators are used to mark the relationship. Different variables or values

Given that x=6 and y=3, the table below explains the logical operators:

Operators         Description            Example

  &&                     and                 (x < 10 && y > 1) is true

  ||                       or                   (x==5 || y==5) is false

   !                       not                 !(x==y) is true

 

Conditional Operators

JavaScript also contains a conditional operato that assigns a value to a variable based on some condition.

Syntax

variablename=(condition)?value1:value2

Example: If the variable age is a value below 18, the value f the variable voteable will be "Too young, otherwise the value of voteable will be "Old enough": 

<html>
<body>
<p>Click the button to check the age.</p>
Age:<input id="age" value="18" />
<p>Old enough to vote?</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var age,voteable;
age=document.getElementById("age").value;
voteable=(age<18)?"Too young":"Old enough";
document.getElementById("demo").innerHTML=voteable;
}
</script>
</body>
</html>
This is the result:

 

 

 

Next 

{--mlinkarticle=2955--}Chapter 12 - JavaScript Conditions{--mlinkarticle--}

 


Viewing all articles
Browse latest Browse all 24

Trending Articles