This is a JavaScript question

Asked 2 years ago, Updated 2 years ago, 15 views

var num = 11;
(num > 100) && num++;
document.writeln ("num: "+num+"<br/>"); //value num: 11

(num < 100) || num++;
document.writeln ("num: "+num+"<br/>");//value num: 11

(num < 100) && num++;
document.writeln ("num: "+num+"<br/>");//value num: 12

(num > 100) || num++;
document.writeln ("num: "+num+"<br/>");//value num: 13

document.writeln("12&1 : "+!(12&1)+"<br/>");//12&1 : true
document.writeln("13&1 : "+!(13&1)+"<br/>");//13&1 : false

The value comes out like this, but I don't know if the value of the variable increases one by one from the third. Can you explain why the last part is "true" and "false"?<

javascript && ||

2022-09-22 19:35

1 Answers

Everything is as you wish...

Left && Right Side

The AND operator omits the evaluation ()execution) of the right side when the left side is false and the result is false even if the right side is false.

Left || Right Side

The OR operator omits the evaluation of the right side when the left side is true and the other side is true.

If you look at each line based on the top:

var num = 11;
(num > 100) && num++; // Left side is false, so right side is omitted
(num < 100) || num++; // Left side is true, so right side is omitted
(num < 100) && num++; // Run to right side because left side is true. num is 12
(num > 100) || num++; // Run to right side because left side is false, num is 13

It's going to be it.

The calculation result of 12 & 1 is 0. && is called logical AND, and & is called bit AND, which is different.

console.log(12..toString(2)) /* "1100" */
console.log(1..toString(2)) /* "1" */
The bit AND result of console.log (12 & 1) /* 1100 and 0001 is 0000 */

End.


2022-09-22 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.