1

I learned that JavaScript is a "loosely-typed" language.

What other languages are "loosely-typed?"

node ninja
  • 31,796
  • 59
  • 166
  • 254
  • That's not even correct English lol. – node ninja Feb 09 '12 at 22:52
  • Loosely typed means you can decide or not decide to give a variable a type (like AS3) as opposed to strong typed where it is mandatory (like Java). There is no construct for declaring a type at all in JS, so it is not any type of "typed", it is untyped. Typed doesn't just mean a language has different types within in, it refers to declaring specific types for a variable itself, such as `String str = 'blah';` in Java or `var str:String = 'blah';` in AS3. – Jimbo Jonny Oct 19 '12 at 16:22

3 Answers3

3

There is disagreement about exactly what "loose" or "weak typing means, however, in so far as commonly understood, "loose typing" refers to a language that has more forgiving typing rules, and might even implicitly convert types from one type to another.

From Wikipedia:

Liskov and Zilles defined a strongly-typed language as one in which "whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function."

According to this definition, JavaScript is loosely-typed (ie. the opposite of strongly typed) because most of JavaScript operators will coerce their operands if necessary.

For example:

[2]-1      // 1 (equivalent to 2-1)
[2]+1      // 21 (equivalent to String([2]) + '1')
true && {} // No error, returns `{}`

Another example of a loosely-typed language example is C.

Note that JavaScript is also dynamically typed. This means that the type of a value is bound to the value and checked at runtime, instead of being bound to the variable and checked at compile time. In effect type checking is performed "dynamically" (ie at runtime) as opposed to "statically" (ie. at compile time).

Examples of dynamic languages:

  • ActionScript
  • Clojure
  • Lisp
  • Lua
  • Perl
  • Python
  • Ruby
  • Smalltalk
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • This is the best answer, because by most definitions javascript is NOT weakly/loosely typed. Strong Typed == must declare a var type (Java). Weak/Loosely Typed == can declare a var type, not mandatory (AS3). Untyped == no option to declare var types (javascript). Defining them as "dynamically typed" is the better way to disambiguate from what many people incorrectly call weak typing, and I applaud you for it. – Jimbo Jonny Oct 19 '12 at 16:17
  • JavaScript is an [excellent example](https://en.wikipedia.org/wiki/Strong_and_weak_typing) of a weakly-typed (aka loosely-typed) language. – Ben Aston Apr 24 '20 at 09:51
3

PHP & PERL are also weakly typed.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

many of the interpreted languages such as php or pearl, in short a loosely-typed language is a language that does not require a variable to be defined