True
In computer programming, true is a boolean value that represents mathematical and logical truth.
True is an important value in computing, because it is the basis of logical operations.
There are two possible boolean values, true and false. If something is true, it is not false; if something is false, it is not true.
"Not false" is not the same as "true." Something may be "not false," and also "not true," if its truth or falsity is unknown.
In many programming languages, either true or True (with an uppercase T) is a reserved word — a special named value that cannot be redefined.
True in JavaScript
In programming, a truth test is used as a conditional statement: "if something is true, do this; otherwise, do that."
In JavaScript, the special value true is implicitly tested in conditional statements. At the beginning of the conditional statement, an expression is provided in parentheses, followed by a block of code. If the expression evaluates as true, the code in the block is executed. If the expression evaluates as false, the code block is skipped.
For example:
var x = 3; // Declare a variable named x, and assign it value of 3. if (x == 3) // Test equality of value of x and 3. If they are equal, // run the code inside the braces { and }. { console.log("x must equal 3, because this line of code is running!"); }
If you run the above code, it outputs the following text to the JavaScript console:
x must equal 3, because this line of code is running!
Notes about the above code:
- In JavaScript, the single equals sign = is an assignment operator. It tells the program, "set the variable on the left to the value on the right."
- The double equals sign == is an equality test. It tells the program, "I want to know if the value on the left equals the value on the right."
- The double slashes // indicate that the rest of the line is a comment. Everything after the double slashes is ignored by the computer when you run the program.
In JavaScript, the double equals sign == represents a "loose" equality test. For example, it treats the floating-point number 5.0 as equal to the integer 5, despite the fact that they are different data types.
The Boolean() function
In JavaScript, the function Boolean(something) returns true if the expression something is true.
For example, if you provide the expression 1 + 1 == 2 ("does one plus one equal two?") to the Boolean() function:
Boolean(1 + 1 == 2);
The function returns the special value true:
true
However, if the expression is not true:
Boolean(1 + 1 == 7);
The function returns false:
false
Below is an example of using the Boolean() function to test if the sum of two integers x and y equals a third integer z.
To use this function, named testTrue(), you must provide it with three integers as the arguments x, y, z. It checks if x + y equals z, and provides the result. If you run this code in a browser, it displays the result in a pop-up alert window.
function testTrue(x, y, z) { alert(`"${x} + ${y} equals ${z}" is ${Boolean(x + y == z)}`); }
For example:
testTrue(1, 2, 3)
Would display the alert message:
You can test this code below. In each of the three text boxes, enter an integer (a whole number). Then, click the button to run testTrue() using those three numbers as x, y, and z.
+ =
If you see "NaN" in your result, that means you typed something that is not a number.
Absolute, Condition, Linux, Programming terms, Valid, Verify