JavaScript question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript question

I’m a beginner in JS so bare with me here. I’m confused about the true and false conditions. If something is true and then something happens because it is true what triggers it? Does it just assume its true and the block runs or like what? I really am hoping for an answer on this so if anyone knows please respond :)

1st Jan 2024, 5:00 PM
Illiana Enow
Illiana Enow - avatar
5 Answers
0
This isn't a bad question. It will help if you have an example though. I'll use an application I've been working on as an example. I want sound enabled by default. I'm using numerical values instead of true or false, but just look at 0 as false and 1 as true. I want sound enabled when the application loads. So... sound = 1 (true) The sounds are activated when a button is clicked, so an IF condition will check to see if the sound is set to 1. It is declared as 1, so a sound will play. I have a toggle that disables sound. So... sound = 0 (false) Now the IF condition is not true so if you click buttons you won't hear a sound. In general terms, "true" and "false" are typically there for reference for other functions. If you have any specific examples I'm sure myself or somebody else here can elaborate.
2nd Jan 2024, 5:55 PM
Ken S
Ken S - avatar
+ 2
Javascript standard defines true and false values as a unique data type
1st Jan 2024, 5:22 PM
Teja
Teja - avatar
+ 1
Do you mean in an "if statement"?
1st Jan 2024, 8:11 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
var condition = true; if(condition) { console.log("It works alright"); } else { console.log("Something went wrong"); } // Output: It works alright This is useful in many fields, one of them: var a = 5; var b = 5; if(a == b) { // a==b is true, so this happens: console.log("equal"); } else { console.log("not equal"); }
1st Jan 2024, 9:44 PM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
0
You can define something as "true" or "false" like so: // Example 1 let state = true; // Our variable 'state' stores the value 'true' if (state) { // If the value stored in 'state' is 'true' execute the following block of code // Some code here } else { // Optional block of code if the value stored in 'state' is falsy ('false', 0, -0, 0n, NaN, null, undefined, ("", '' - empty strings)) // Some code here } // Example 2 let a = 5; let b = 8; let isGreaterA = a > b; // Check whether 'a' is greater than 'b' or not, and set the value (true or false) to 'isGreaterA' variable if (isGreaterA) { // If value stored in 'isGreaterA' is true execute the following block of code console.log("a is greater than b"); } else { // Optional (Since the above statement stored in 'isGreaterA' is false, execute the following code) console.log("a is greater or equal to b"); } // Output: a is greater or equal to b // Example 2 let a = 5; let b = 8; if (a > b) { console.log("a is greater than b"); } else if (b > a) { console.log("b is greater than a"); } else { console.log("a and b are equal"); } if ((a + b) > 15) { // Checks 13 > 15 and the statement is false console.log("Sum of a + b is greater than 15"); } else if ((a + b) > 10) { // Checks 13 > 10 and the statement is true console.log("Sum of a + b is greater than 10"); } // Output: Sum of a + b is greater than 10
4th Jan 2024, 7:32 PM
Boris Batinkov
Boris Batinkov - avatar