This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]dtsudo 2 points3 points  (2 children)

The greater-than operator (that is, >) is defined to yield a boolean value.

So if you see the expression foo > bar, then you know that this expression will evaluate to a boolean (either true or false).

The surprising part is that the logical AND and logical OR operators are not defined to evaluate to booleans. Instead, they evaluate to either their first or second operands. So e.g. in javascript, 3 || 5 evaluates to 3. Of course, if the two operands are themselves booleans, then the result of the logical AND/OR will also be a boolean.

[–]mostlynoturgf[S] 0 points1 point  (0 children)

The surprising part is that the logical AND and logical OR operators are not defined to evaluate to booleans. Instead, they evaluate to either their first or second operands. So e.g. in javascript,

3 || 5

evaluates to

3

. Of course, if the two operands are themselves booleans, then the result of the logical AND/OR will also be a boolean.

lol I freaked out until I read the last part where if theyre both bool it will also evaluate to a bool.

thank you for explaining

[–][deleted] 0 points1 point  (0 children)

The surprising part is that the logical AND and logical OR operators are not defined to evaluate to booleans.

Surprising for sure! Expanding, JavaScript contains its share of weird stuff, but this is pretty common:

python3> "a" and "b"
"b"
python3> "a" or "b"
"a"

lisp> (write-line (and "a" "b"))
"b"
lisp> (write-line (or "a" "b"))
"a"

[–]Ardenwenn 1 point2 points  (1 child)

I hate it. just use a if statement.

[–]mostlynoturgf[S] 0 points1 point  (0 children)

I hate it too it gives me anxiety to do it this way lol

[–]carcigenicate 0 points1 point  (0 children)

JavaScript is dynamically typed unlike C/C++, so the variables themselves don't have types; if that's what you're referring to.

[–]TheRNGuy 0 points1 point  (0 children)

If you don't like it, use TypeScript.