Strong vs weak typing

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the types category.

Last Updated: 2024-03-02

Ruby is dynamically and strongly typed where as JavaScript is dynamically and weakly typed. What is this strong vs weak distinction?

Basically Ruby will let you do this:

x = "3"
y = x + "foo!" # "3foo"

but not this:

x = "3"
y = x + 3 #BOOM

i.e. if you start mixing types together in an expression (here: string and Fixnum), it will cause an exception.

However, JavaScript -- being weakly typed -- will attempt to make this work:

x = "3"
y = x + 3 // Gives "33"