# Is "0 <= x <= 5" At All Possible?

#1

Hi, Everyone,

So “greater than or equal to” in JavaScript is “>=” while “less than or equal to” is “<=”…but is there any way to “sandwich” a value like in algebra, to write something like “0 < X < 5” – or is there no such syntax at all in JavaScript (meaning I’d have to resort to the inelegant “X >= 0 && X <= 5”)?

Thanks!

#2

Not in JavaScript, no.

You have no choice but to use `&&`.

Ideally, comparisons are made in the same direction.

``if (0 <= x && x <= 5) {}``

#3

not possible, then javascript evaluate from left to right, first JS would compare `0 < x`, this gives either true or false (depending on value of x), then true or false is compared with 5 (`true < 5` or `false < 5`), who knows how that will evaluate

you will have to do it the ugly way.

#4

We know that, `true`. I’m knackered trying to prove this, but JS sees `true` as `1` even though it won’t recognize it as an integer the way Python does. Gotta be some C going on in the background. `false` is seen as `0`.

OR short-circuits on `true` so the first expression kicks us out and the second is not evaluated (but it too is `true`).

#5

Computer languages are so fascinating that it’s very easy to get sidetracked (get away from actually actively [through actual coding] learning to program) and read up on their design and even their very physical possibility (an electric pulse is 1, no pulse is 0) – and that’s just the non-quantum aspects!

But yes, I wonder what’s happening under the hood that makes something so fundamentally mathematical not “compute” (pun intended!) in JavaScript, something like 0 < x < 5…

#6

Try this in Node.js

``````x = 4

0 < x < 5    // true
``````

But that is because Node.js is a C implementation, as I understand it.

#7

I don’t know nothing about no node.js except that it’s server-side, right?

Well, anyway, thanks…obviously, I’ve got a whole lot more to learn before any “under the hood” advice can be comprehensible to me!

#8

Actually, both client and server. Node.js permits us to run JS as a standalone (without a browser). When you get comfortable working with script, get yourself a free copy and install it on your machine. Personally, I don’t reach for it much unless I just want to run a .js file. Mostly I work in the JavaScript console, or in a webpage.

Really hone your HTML skills, so that you can smoothly incorporate script in your pages. CSS is also very well married to JS, which is where all the cool dynamic behaviors come from. BUT, CSS can do a lot more than people may realize. Simple visual effects and animation can be done exclusively with CSS (though can be easily emulated with script). I always suggest to people that they write pages the work without script. There is so much we can now do with CSS so when you get to that study, explore down every road. CSS Tricks is a great site for seeing how far CSS has come these past few years. QuirksMode is a good site for picking up on the history of browser API’s and the quirks in various iterations. Keep MDN close at hand for anything to do with browsers. Follow the links from their pages to W3C and take a few minutes every day to familiarize yourself with both sites.

#9

Indeed! I’ve got these sites bookmarked for “inspiration:”

https://waaark.com/
https://pesticide.io/

Hey, nice – thanks for the info!

#10

You’re welcome!

Side note: Node.js is the backbone of several frameworks, and not just a minor player. It is a direct link from ES to C, and clearly the industry has leveraged this bridge. It should not be treated lightly.

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.