DEV Community

Cover image for Is JavaScript Statically Typed or Dynamically Typed? πŸ€”
Jagroop Singh
Jagroop Singh

Posted on

Is JavaScript Statically Typed or Dynamically Typed? πŸ€”

JavaScript – the language that powers the web! But when it comes to its typing system, people often wonder: Is JavaScript statically typed or dynamically typed? Let’s dive in, but with a twist – through examples, not just theory. 😎

What Does "Typed" Mean Anyway? 🧐

Before we jump into JavaScript, let’s quickly refresh what it means when we say a language is "typed."

  • Statically Typed: Variables must be declared with a specific type. Once you set the type, it cannot change.
  • Dynamically Typed: The type of a variable is determined at runtime, and it can change over time.

JavaScript's Type System πŸ–₯️

JavaScript is dynamically typed. πŸŽ‰ This means you can assign any type of value to a variable, and you don't need to explicitly declare the type.

Example 1: Variable Type Flexibility πŸ”„

let x = 5;       // x is a number
console.log(typeof x);  // "number"

x = "Hello!";    // Now x is a string
console.log(typeof x);  // "string"
Enter fullscreen mode Exit fullscreen mode

In the above example, x starts as a number, and then we change it to a string. JavaScript allows this fluidity because it is dynamically typed.

Example 2: Function Argument Types πŸ”§

function printInfo(info) {
  console.log(info);
}

printInfo(42);      // Prints 42 (number)
printInfo("Hello"); // Prints Hello (string)
Enter fullscreen mode Exit fullscreen mode

Notice how the info argument can accept any type of data β€” a number, a string, an object, etc. JavaScript doesn’t require you to specify what kind of value info will hold when you call printInfo. The language decides at runtime.

Example 3: The Type β€œSurprise” 🎁

let value = 10;
value = value + "5";  // Adding a string to a number
console.log(value);    // "105" (String, not a number!)
Enter fullscreen mode Exit fullscreen mode

In this case, JavaScript automatically converts the number 10 into a string and concatenates it with "5". The type transformation happens seamlessly, which might surprise you, right? 😱

Example 4: Even Arrays and Objects Have No Restrictions! πŸ› οΈ

let user = {
  name: "Alice",
  age: 25
};

user = [1, 2, 3]; // Reassigned as an array!
console.log(user);  // [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

We started with an object, then re-assigned the variable user to be an array. In statically typed languages, this would throw an error, but JavaScript handles it like a pro.


So... Why Is JavaScript Dynamically Typed? πŸ”

JavaScript doesn't force you to declare types, making it more flexible and easier to write code quickly. However, this flexibility can also lead to unexpected behavior, especially when dealing with complex data and functions.

The Flip Side βš–οΈ

This flexibility comes with a catch: bugs can be harder to spot because types can change at runtime. It’s your responsibility as a developer to keep track of what kind of values your variables hold, or you might encounter some weird results. πŸ‘€


Conclusion: To Type or Not to Type? πŸ€·β€β™‚οΈ

To wrap things up: JavaScript is dynamically typed! It lets you be flexible with your code, but also, sometimes unpredictable. πŸ˜…

Now, here's the tricky question for you... If you could choose, would you prefer JavaScript to be statically typed? And if so, would that improve or limit your coding experience? πŸ’‘

Top comments (3)

Collapse
 
pengeszikra profile image
Peter Vivo

For example hard to exactly typed a dynamical changed DOM query result, because the if the given tag type is different then the corresponding attributes and function list also different.

Also problematic set exact type to the generator functions because that may have a different return values on different yield.

Also will be problematic if user pass a complex maybe union types to a module which is according this generic-union give back another generic types related to user defined one. Like my jsdoc-duck module does.

Collapse
 
jeffrey_tackett_5ef1a0bdf profile image
Jeffrey Tackett

The way to visualize dynamically typed languages like JavaScript, Python, etc., you have to understand that the value is what retains the type instead of the variable name. When you understand this, you then can grasp the complexity of JavaScript type conversion -- which is something that most languages avoid and instead require the developer to use conversion functions/methods. JavaScript was made for automating forms and with that the need for it to do it's best to not fail leaf the team to create the conversation rules that dictate what type will be returned when two different types are used.

That is why every month or so another article is written about this behavior, as it is the most unique experience in programming and has been a source of many bugs for those who don't understand how it determines a type.

Collapse
 
wfreeth profile image
waynef

Love dynamic typing, once you know the rules you can build stuff so much faster without the type gymnastics.