+ 5
Js question âď¸
Hey đ I watched YouTube tutorials and tried much but i cannot understand comparison operators and logical and Boolean operaters Please explain clearly with examples đŹ
3 Answers
+ 5
About logical we have
&& - and
|| - or
! - not
Imagine next code
var x = 15;
// true && true - if both side are true it will return true
if ( x > 10 && x < 20) {
console.log("x are between 10 and 20);
}
This is one usecase of &&
For || you can check
var x = 13;
var y = 9;
// true || false - will return true and code inside if will run
if (x > 10 || y > 10) {
console.log("one of x or y are greater then 10)
}
Examle for ! - not
var x = 10;
var y = 8;
!(false) -- will be true and code will run, i didnt used this one too much
if (!(x==y)) {
console.log("something")
}
One usecase for ! - not is if you make some button what need to switch from true to false when clicked, you can say
var clickedBtn = false;
// in function where button is clicked you set
clickedBtn = !clickedBtn;
// it will be oposite, so if button was false it will be true, if it was true it will be false..
+ 5
To compare 2 value we have:
value1 == value2, it will check are both value same, but it will also do type convertion
1 == 1 is true,
1 == "1" is also true
value1 === value2 will check are value is same, but it wont convert type.
1 === 1 is true
1 === "1" is false
We also can check are some value <, >, <=, >=
We also can use != to check if something is not equal, also !== will check if something is not indentical.
Here is good documentation about them
https://www.w3schools.com/js/js_comparisons.asp
+ 5
PanicS thank you đ