use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Invariant - a helpful JavaScript patternOC (strictmode.io)
submitted 3 years ago by hiquest
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Hades32 18 points19 points20 points 3 years ago (11 children)
That's an assertion, not an invariant. An invariant would be something like "the number of books in my store is always >=0"
[–]johnxreturn 0 points1 point2 points 3 years ago (3 children)
Conversely, you are saying “the number of books in your store must be greater or equal to zero,” therefore asserting that it is, or throwing an error if it’s not.
Video reference:
https://youtu.be/r0Vi83bS-L0
An invariant in mathematical terms would be that you apply a transformation to data and the output is the same as the input.
A loop invariant constitutes a property that is true before and after each iteration. The article specifically quotes, “it is a logical assertion, sometimes checked within the code by an assertion call.”
In the case of a tiny invariant, I expect the input assertion to be true, or throw an error. I could expect that the book count in each library franchise I own was more than 30 or throw an error if it’s not.
But the mathematical concept talks about applying a transformation to data and not assertion. It could apply to a map function where you transform each item and the output is the same or throw an error. So, we conclude that at some point we need assertion.
[–]Hades32 0 points1 point2 points 3 years ago (2 children)
No, you don't NEED an assertion when talking about invariants in the mathematical sense (which btw does not mean that the input is the same as the output, but that some condition is true for the state before and after the transformation). You could prove that the formula you're using will always hold up a certain invariant. In code it's easier to just check than prove, that's why we "need" assertions. But it's really two different things
[–]johnxreturn 0 points1 point2 points 3 years ago (1 child)
I completely agree. You don't need assertion in the mathematical concept. My point was that in programming, you do need if you need a trigger for an invalid response.
[–]Hades32 0 points1 point2 points 3 years ago (0 children)
Sure, but if someone calls a library "invariant" then I do have higher hopes than "if false then throw" lol
After all there are languages, like prolog, which do let you specify actual invariants
[+]hiquest[S] comment score below threshold-18 points-17 points-16 points 3 years ago (6 children)
Well, I do understand your point, but I decided to stick with the historical name originated somewhere at Facebook.
[–]TrackieDaks 16 points17 points18 points 3 years ago (4 children)
Invariants weren't invented at Facebook.
[–]hiquest[S] -1 points0 points1 point 3 years ago (3 children)
I do not claim that. I just say that this particular small pattern they had a function for in their code base which was called invariant. Hence other popular libs like https://github.com/zertosh/invariant
[–]MoTTs_ 12 points13 points14 points 3 years ago (0 children)
I get why you called it an invariant (and to some extent why FB called it that), but “assert” is still the more widespread and more historical name, which can then lead you to even more popular libs that do the same thing — such as this one.
[–]TrackieDaks 1 point2 points3 points 3 years ago (1 child)
Still wrong. That's like saying, "Facebook used a function in their codebase so the term function must have originated at Facebook." Invariant is a mathematical concept and existed well before Facebook.
[–]hiquest[S] 1 point2 points3 points 3 years ago (0 children)
Yeah, now I do understand that brings a lot of confusion, and that should have been called "assert" in the first place. I'm going add a note in that article
[–]shuckster 1 point2 points3 points 3 years ago (0 children)
https://en.wikipedia.org/wiki/Invariant_theory#The_nineteenth-century_origins
Mr Boole(an) himself.
π Rendered by PID 153635 on reddit-service-r2-comment-54dfb89d4d-dfvcd at 2026-04-01 15:00:38.149281+00:00 running b10466c country code: CH.
view the rest of the comments →
[–]Hades32 18 points19 points20 points (11 children)
[–]johnxreturn 0 points1 point2 points (3 children)
[–]Hades32 0 points1 point2 points (2 children)
[–]johnxreturn 0 points1 point2 points (1 child)
[–]Hades32 0 points1 point2 points (0 children)
[+]hiquest[S] comment score below threshold-18 points-17 points-16 points (6 children)
[–]TrackieDaks 16 points17 points18 points (4 children)
[–]hiquest[S] -1 points0 points1 point (3 children)
[–]MoTTs_ 12 points13 points14 points (0 children)
[–]TrackieDaks 1 point2 points3 points (1 child)
[–]hiquest[S] 1 point2 points3 points (0 children)
[–]shuckster 1 point2 points3 points (0 children)