Interval comparison doesn't bomb in JS
Why doesn't interval comparison bomb in JavaScript?
if(-1 < x < 1)
console.log('x: ', x)
Why are we allowed to do this without getting errors?
Also it seems that:
-1 < x < 1
is true forx<=-1
1 < x < 1
is true forx<=1
-1 < x < -1
is always false-2 < x < 2
is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
javascript boolean intervals
add a comment |
Why doesn't interval comparison bomb in JavaScript?
if(-1 < x < 1)
console.log('x: ', x)
Why are we allowed to do this without getting errors?
Also it seems that:
-1 < x < 1
is true forx<=-1
1 < x < 1
is true forx<=1
-1 < x < -1
is always false-2 < x < 2
is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
javascript boolean intervals
1
They are evaluated as(-1 < x) < 1
, which is equivalent to(-1 < x) != true
or!(-1 < x)
– Bergi
Nov 15 '18 at 13:15
What you really need is-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34
add a comment |
Why doesn't interval comparison bomb in JavaScript?
if(-1 < x < 1)
console.log('x: ', x)
Why are we allowed to do this without getting errors?
Also it seems that:
-1 < x < 1
is true forx<=-1
1 < x < 1
is true forx<=1
-1 < x < -1
is always false-2 < x < 2
is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
javascript boolean intervals
Why doesn't interval comparison bomb in JavaScript?
if(-1 < x < 1)
console.log('x: ', x)
Why are we allowed to do this without getting errors?
Also it seems that:
-1 < x < 1
is true forx<=-1
1 < x < 1
is true forx<=1
-1 < x < -1
is always false-2 < x < 2
is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
javascript boolean intervals
javascript boolean intervals
asked Nov 15 '18 at 13:12
Andrea BergonzoAndrea Bergonzo
5171618
5171618
1
They are evaluated as(-1 < x) < 1
, which is equivalent to(-1 < x) != true
or!(-1 < x)
– Bergi
Nov 15 '18 at 13:15
What you really need is-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34
add a comment |
1
They are evaluated as(-1 < x) < 1
, which is equivalent to(-1 < x) != true
or!(-1 < x)
– Bergi
Nov 15 '18 at 13:15
What you really need is-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34
1
1
They are evaluated as
(-1 < x) < 1
, which is equivalent to (-1 < x) != true
or !(-1 < x)
– Bergi
Nov 15 '18 at 13:15
They are evaluated as
(-1 < x) < 1
, which is equivalent to (-1 < x) != true
or !(-1 < x)
– Bergi
Nov 15 '18 at 13:15
What you really need is
-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
What you really need is
-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34
add a comment |
3 Answers
3
active
oldest
votes
Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x
results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1
part. So:
When
-1 < x
is false, the second part is0 < 1
which is true.When
-1 < x
is true, the second part is1 < 1
which is false.
This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.
-1 < x < -1 is always false
-2 < x < 2 is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
Using x = -1
and x = 1
:
- If
x = -1
, then-1 < x
is false, so the rest is0 < -1
, which is false. - If
x = 1
, then-1 < 1
is true, so the rest is1 < -1
which is false. - If
x = -1
, then-2 < -1
is true, so the rest is1 < -2
, which is false. - If
x = 1
, then-2 < 1
is true, so the rest is1 < -2
which is false.
add a comment |
I think that this happens because javascript implicitly considers true
to have the value 1
and false
to have the value 0
.
When you do -1 < x < 1
, what you're actually doing is (-1 < x) < 1
, or true < 1
if x = 0
which is false
.
However, if x=-2
, (-1 < x < -1)
will return true
as false < 1
is true
. Hope this helps.
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
add a comment |
The reason is that JS interprets your expression.
if((-1 < x) < 1)
console.log('x: ', x)
Trying using braces...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320295%2finterval-comparison-doesnt-bomb-in-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x
results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1
part. So:
When
-1 < x
is false, the second part is0 < 1
which is true.When
-1 < x
is true, the second part is1 < 1
which is false.
This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.
-1 < x < -1 is always false
-2 < x < 2 is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
Using x = -1
and x = 1
:
- If
x = -1
, then-1 < x
is false, so the rest is0 < -1
, which is false. - If
x = 1
, then-1 < 1
is true, so the rest is1 < -1
which is false. - If
x = -1
, then-2 < -1
is true, so the rest is1 < -2
, which is false. - If
x = 1
, then-2 < 1
is true, so the rest is1 < -2
which is false.
add a comment |
Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x
results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1
part. So:
When
-1 < x
is false, the second part is0 < 1
which is true.When
-1 < x
is true, the second part is1 < 1
which is false.
This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.
-1 < x < -1 is always false
-2 < x < 2 is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
Using x = -1
and x = 1
:
- If
x = -1
, then-1 < x
is false, so the rest is0 < -1
, which is false. - If
x = 1
, then-1 < 1
is true, so the rest is1 < -1
which is false. - If
x = -1
, then-2 < -1
is true, so the rest is1 < -2
, which is false. - If
x = 1
, then-2 < 1
is true, so the rest is1 < -2
which is false.
add a comment |
Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x
results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1
part. So:
When
-1 < x
is false, the second part is0 < 1
which is true.When
-1 < x
is true, the second part is1 < 1
which is false.
This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.
-1 < x < -1 is always false
-2 < x < 2 is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
Using x = -1
and x = 1
:
- If
x = -1
, then-1 < x
is false, so the rest is0 < -1
, which is false. - If
x = 1
, then-1 < 1
is true, so the rest is1 < -1
which is false. - If
x = -1
, then-2 < -1
is true, so the rest is1 < -2
, which is false. - If
x = 1
, then-2 < 1
is true, so the rest is1 < -2
which is false.
Because JavaScript allows implicit type coercion, in this case from boolean to number. The -1 < x
results in a boolean, which is then implicitly coerced to a number (true = 1, false = 0) for the (result) < 1
part. So:
When
-1 < x
is false, the second part is0 < 1
which is true.When
-1 < x
is true, the second part is1 < 1
which is false.
This is covered in the abstract relational comparison algorithm in the spec, and the various operations it links to.
-1 < x < -1 is always false
-2 < x < 2 is always true
In the last 2 cases it seems it is just comparing the 2 ends of the expressions. How are those expressions evalued?
Using x = -1
and x = 1
:
- If
x = -1
, then-1 < x
is false, so the rest is0 < -1
, which is false. - If
x = 1
, then-1 < 1
is true, so the rest is1 < -1
which is false. - If
x = -1
, then-2 < -1
is true, so the rest is1 < -2
, which is false. - If
x = 1
, then-2 < 1
is true, so the rest is1 < -2
which is false.
edited Nov 15 '18 at 13:20
answered Nov 15 '18 at 13:15
T.J. CrowderT.J. Crowder
694k12212381330
694k12212381330
add a comment |
add a comment |
I think that this happens because javascript implicitly considers true
to have the value 1
and false
to have the value 0
.
When you do -1 < x < 1
, what you're actually doing is (-1 < x) < 1
, or true < 1
if x = 0
which is false
.
However, if x=-2
, (-1 < x < -1)
will return true
as false < 1
is true
. Hope this helps.
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
add a comment |
I think that this happens because javascript implicitly considers true
to have the value 1
and false
to have the value 0
.
When you do -1 < x < 1
, what you're actually doing is (-1 < x) < 1
, or true < 1
if x = 0
which is false
.
However, if x=-2
, (-1 < x < -1)
will return true
as false < 1
is true
. Hope this helps.
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
add a comment |
I think that this happens because javascript implicitly considers true
to have the value 1
and false
to have the value 0
.
When you do -1 < x < 1
, what you're actually doing is (-1 < x) < 1
, or true < 1
if x = 0
which is false
.
However, if x=-2
, (-1 < x < -1)
will return true
as false < 1
is true
. Hope this helps.
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
I think that this happens because javascript implicitly considers true
to have the value 1
and false
to have the value 0
.
When you do -1 < x < 1
, what you're actually doing is (-1 < x) < 1
, or true < 1
if x = 0
which is false
.
However, if x=-2
, (-1 < x < -1)
will return true
as false < 1
is true
. Hope this helps.
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
edited Nov 15 '18 at 13:25
answered Nov 15 '18 at 13:20
theapologisttheapologist
576215
576215
add a comment |
add a comment |
The reason is that JS interprets your expression.
if((-1 < x) < 1)
console.log('x: ', x)
Trying using braces...
add a comment |
The reason is that JS interprets your expression.
if((-1 < x) < 1)
console.log('x: ', x)
Trying using braces...
add a comment |
The reason is that JS interprets your expression.
if((-1 < x) < 1)
console.log('x: ', x)
Trying using braces...
The reason is that JS interprets your expression.
if((-1 < x) < 1)
console.log('x: ', x)
Trying using braces...
answered Nov 15 '18 at 13:20
Ram VenkatRam Venkat
787
787
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320295%2finterval-comparison-doesnt-bomb-in-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
They are evaluated as
(-1 < x) < 1
, which is equivalent to(-1 < x) != true
or!(-1 < x)
– Bergi
Nov 15 '18 at 13:15
What you really need is
-1 < x && x < 1
– yunzen
Nov 15 '18 at 13:24
@HerrSerker I know how to do proper comparisons, I was just asking how JS evaluated what in other languages is called interval comparisons.
– Andrea Bergonzo
Nov 15 '18 at 13:59
I knew this. But it would be interesting to other people who want to know, how it's done right
– yunzen
Nov 15 '18 at 14:34