DEV Community

loading...

Discussion on: What **is** JavaScript? 🤷🏼‍♀️

Collapse
ricobrase profile image
Rico Brase

About the dynamically typed thing:
You are describing type inference instead of dynamic typing.

Type inference means, that the runtime/compiler infers the type of a variable/parameter from the current context (the "it knows" part).

example (JS):

var hello = "world"; // string
Enter fullscreen mode Exit fullscreen mode

example (C#):

var hello = "world"; // string
Enter fullscreen mode Exit fullscreen mode

Dynamically typed on the other hand means, that the type of the variable/parameter can be changed during runtime and is not bound to a specific type. In most cases, it's used with type inference, allowing for confusion.

example (JS):

var hello = "world"; // string
hello = 42; // number
Enter fullscreen mode Exit fullscreen mode

example (C#):

var hello = "world"; // string
hello = 42; // COMPILER ERROR, this is NOT SUPPORTED by default in C# since hello is of type string!
Enter fullscreen mode Exit fullscreen mode

C# DOES support dynamic typing, but it's required to opt-in per variable/parameter using the dynamic keyword. Please note that for most use cases, this is considered bad practice since compiler type checking DOES prevent a lot of errors in development BEFORE publishing your application to the public.
(Errors are caught during development and don't crash the application for the user in runtime.)

dynamic hello = "world"; // string
hello = 42; // int
Enter fullscreen mode Exit fullscreen mode
Collapse
javascriptcoff1 profile image
JavaScript➕Coffee🚀 Author

Thanks! I know that C# DOES technically allow for dynamically typed, hence the caveat, but this article is for codenewbies and I couldn't think of another language to explain it with or get examples from!