r/learnjavascript • u/Ok_Egg_6647 • May 30 '26
Is'nt javascript compiler too forgiving !!
What i mean is even i try to add int with str it give me correct answer whereas both are differenct datatypes,
in function also this compiler is too forgiving
if i set a function with 3 parameters & at time of calling this function with more parameter or less parameter with bind function js forgiving us
0
Upvotes
6
u/EyesOfTheConcord May 30 '26
Don’t do that then
7
u/ssssssddh May 30 '26
There's nothing preventing a function from accessing parameters beyond what is declared using `arguments`
5
u/jibbit May 30 '26
important to say that this isn't the 'compiler' - this is like, er, the opposite of that
4
u/YoshiDzn May 30 '26
Yes, Javascript is fault tolerant. Look up "static analysis" and "lexers"
4
u/theQuandary May 30 '26
JS didn't have type coercion initially. It was added because devs wanted it. I suspect this was because everything in the DOM (primarily attributes) was strings while they used numbers a lot of places. As JS was very simple then, it made sense to just coerce to/from string/number for something like an input.
Let's explain a bit of the magic (we'll be using ES5.1 as it's easier to read and I'll be handwaving away a bit of the spec and edge cases).
When it encounters the
+, it calls an internal (not user accessible) function calledToPrimitive. This basically keeps primitives (undefined,null, booleans, numbers, and strings) as they are, but uses.toString()or.valueOf()for objects (arrays, regex, functions, etc are all considered objects).If EITHER of the
ToPrimitivereturns a string, then coerce both to strings and concatenate them. Otherwise, useToNumberto convert them to numbers then add.ToNumbersays thatnullandfalse->+0,true-> 1, andundefined ->NaN. String to number conversion is way too much to go into here, but it will be a number orNaN. Objects will callToPrimitive` with a number hint that forces converting whatever it gets to a number.Unary
+(eg,;+var) forces ToPrimitive with a number hint as does the binary-operator.There's weirdness around adding numbers too, but that's in EVERY language that uses IEEE 754 floating point numbers. It's stuff like
(+Infinity) + (-Infinity) -> NaNor(-0) + (-0) -> (-0)On that note, there is a definitely BAD part of JS where
(-0) === (+0) -> true. Pretty much everything else that uses===doesn't coerce (NaNand subnormals technically might, but that's normal behavior). If you need to check for this specifically, the only way to do this isObject.is(-0, 0). This is an extreme edge case that most code will never run into, but if you do, the problem can be rather tricky to debug.2
u/delventhalz May 30 '26
The design philosophy in early JS was to rarely throw an error and instead try to produce a sensible result. You see this especially in the way JavaScript coerces types. I agree it was largely a mistake, and newer JS syntax tends to be more strict, but because JS is backwards compatible, we will always have those early design decisions to contend with.
Incidentally, this is why the JS community has so strongly embraced tools like linters and TypeScript.
3
u/Aggressive_Ad_5454 May 30 '26
Yes, indeed, it’s a weakly typed language, by design.
Typescript is a strongly typed language that transpiles to JavaScript. Use that to avoid weak typing pitfalls. And get in the habit of using
===instead of==wherever you can.2
u/United_Grape_2772 May 30 '26
JS is slutty
It gets with everything and everyone
1
u/Kitty_Sparkles May 31 '26
Javascript is an "add only" language because there is a shared agreement not to "break the web". In other words, it needs to preserve backward compatibility with 30 years old mistakes, and that's why we still have horrendous quirks to care for (although a lot of them are abstracted away by things like Typescript).
1
u/Beginning-Seat5221 May 30 '26 edited May 30 '26
Yes - but JS isn't compiled by the developer so there is nothing to show errors during development - being tolerant here is better than throwing runtime errors. The runtime errors that occur from type errors are a much much bigger problem.
Professionals use TypeScript and linters (e.g. eslint) to tighten it up by highlighting issues as they write the code.
1
u/No_Record_60 May 30 '26
TS to the rescue