1

JavaScript is said to be a "loosely-typed" language. This is due to the fact that the runtime allows operations to be performed on operands of different types (via coercion):

var number = 6;
var bool = true;
var result = number + bool; //result is 7

Coming from a mostly statically-typed, strongly-typed background, I am having a hard time reasoning about the benefits of this type of approach. Sure, it can make for some pretty concise syntax, but it also seems like it could cause a nightmare when trying to track down bugs. So, besides conciseness, what are some of the benefits of loose typing and implicit type conversions?

TMcManemy
  • 814
  • 9
  • 20
  • I'm personally not a fan of javascript's implicit type conversions, it is often very [unintuitive](http://stackoverflow.com/questions/7202157/why-is-10) – simonzack Aug 11 '14 at 02:10
  • 3
    Check [What arguments are there in favor of weak typing?](http://programmers.stackexchange.com/questions/38002/what-arguments-are-there-in-favor-of-weak-typing) – Ram Aug 11 '14 at 02:11
  • coming from a loose-type background, i've always wondered why a smart computer would need dumb ole me to spell out the difference between 1, 1.0, and "1"; the bugs you refer to are highly over-hyped by classical programmers, and represent a small subset of the most common potential bugs. yeah, js may have a some humorous demonstrations of the side-effects of this conversion, but it's not much of a problem for serious coders. besides, don't even old programmers know that 0==false and 1==true? – dandavis Aug 11 '14 at 02:13
  • _"This is due to the fact that the runtime allows operations to be performed on operands of different types"_ - That's not what makes it loosely typed, because even (some) strongly typed languages allow that (for certain operations). – nnnnnn Aug 11 '14 at 02:32
  • @nnnnnn I thought the main distinction between loosely and strongly typed languages was the willingness of the runtime to perform implicit type conversions. Am I mistaken? If so, what feature(s) would make a language loosely-typed? – TMcManemy Aug 11 '14 at 02:39
  • Well perhaps I'm mixing up "loosely" typed with "weakly typed" and "dynamically typed". – nnnnnn Aug 11 '14 at 02:45
  • I'm confused as to why this question was put on hold, while many other "pros/cons" questions exist on SO. If the answer to this question is purely subjective (e.g. there is _no_ real benefit to implicit type conversions, it's just a matter of personal preference), I would accept that notion as an answer. – TMcManemy Aug 11 '14 at 12:27
  • The other pros/cons questions shouldn't really be on SO either, but sometimes older questions are kept for posterity, and sometimes the criteria for what is too subjective is also subjective. In my opinion your particular question makes more sense over at http://programmers.stackexchange.com/ as per the existing question there that undefined linked to (above). – nnnnnn Aug 11 '14 at 23:25
  • @nnnnnn Thanks for following up. – TMcManemy Aug 12 '14 at 19:18

1 Answers1

4

Loosely typed languages have a number of differences which can be taken as advantages:

  • There is no need of interfaces. As long as an object has the method name that you need, call that method. Not using interfaces can simplify coding and reduce code size.
  • There is no need of generics, for very similar reasons.
  • "by type" function overloads are handled more simply If a function needs a string parameter, then just cast the incoming value to a string. If type checking is needed, it can be added there.
  • We don't have, or need, classes. The [almost] everything is an object makes passing values around much easier. No need to auto-box, no need to cast values coming out.
  • Objects are easily extended without breaking code. You can create an array then drop replace the indexOf method to use one uses the binary search. The end result is smaller, and IMHO, cleaner code.
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • 1
    Then you need to write lots of TDDs for catching type mismatch on runtime :) then you will have lots of `undefined method reference errors` haha :D I personally love statically typed languages. It makes me more productive and code becomes less buggy – Jama A. Aug 11 '14 at 02:22
  • @JamaJurayevich Passing the wrong values in has never really been an issue for me, or any of the teams that I've worked with... then again, I cut my teeth on assembly. – Jeremy J Starcher Aug 11 '14 at 02:25
  • @JeremyJStarcher Wouldn't points #1, 2, and 5 be more accurately attributed to dynamically-typed languages (not loosely-typed languages)? – TMcManemy Aug 11 '14 at 02:36
  • @TMcManemy You have a point and I should have been specific there. I fell into the trap of `loosely-typed` being over-applied. Thanks. – Jeremy J Starcher Aug 11 '14 at 02:40