LEARNING THE FUNDAMENTAL

Javascript Fundamentals: Understanding why it is a DYNAMIC and WEAK language

Frontend Roadmap: Javascript Part II

Code & Cozy
5 min readJun 20, 2023
Image from Abel Escobar in Pixabay

Before explaining the whole universe of data types in Javascript, we need to continue to understand the syntax or the kind of language that it is javascript.

In the last post, we mentioned that Javascript is a high-level programming language, which basically means that Javascript was designed to be easy for humans to read, write, and understand.

So today, we will learn two more concepts related to the core of Javascript.

Javascript is a dynamic and weak language. And this doesn’t mean about his character or personality :). So let’s go deep into more about this.

What does Dynamic Language mean?

JavaScript is a dynamic language and not static, which means that variables can hold values of different types during runtime. Unlike languages such as Typescript or Java, you don’t need to declare the data type of a variable explicitly.

let x = 5; // x is a number
x = "Hello"; // x is now a string
x = [1, 2, 3]; // x is now an array

In the above example, the variable xcan hold a number, a string, or an array at different points in time. JavaScript allows this flexibility without requiring type annotations or declarations.

If you know languages such as Typescript (that is more a superset of Javascript) or Java, here is the same example but using a static language program, the other side of the coin.

Typescript’s example:

let x: number = 5; // x is explicitly declared as a number
x = "Hello"; // Error: Type 'string' is not assignable to type 'number'

In TypeScript, you explicitly declare the type of a variable using type annotations. This example explicitly declares it as a number using the : number type annotation. When you try to assign a string to x, TypeScript detects the type mismatch and throws a compilation error.

Java’s example:

int x = 5; // x is an int
x = "Hello"; // Error: incompatible types: String cannot be converted to int

In Java, you declare variables with specific types using keywords such as int, String, etc. Similar to TypeScript, when you attempt to assign a string to an integer variable (x), the Java compiler detects the type mismatch and raises a compilation error.

And what does Weakly-Typed Language mean?

JavaScript is also a weakly typed language, which means that the language allows you to perform operations between different data types without explicitly converting them.

For example:

let num = 5; // num is a number
let str = "10"; // str is a string

let result = num + str; // JavaScript performs implicit type coercion
console.log(result); // Output: "510"

In the above example, we have a number 5 stored in the variable num, and a string "10" stored in the variable str. Normally, when you try to add a number and a string, you expect an error because they are different data types. However, JavaScript performs implicit type coercion, which automatically converts the number to a string and performs the operation.

So, in this case, JavaScript converts the number 5 to a string and concatenates it with the string "10", resulting in the string "510". The conversion of the number to a line happens implicitly, without you explicitly instructing JavaScript to do so.

However, implicit type coercion can sometimes lead to unexpected results if you’re not careful. It’s important to be aware of how JavaScript handles these conversions to avoid any unintended consequences in your code.

And, the same as the previous section, If you know languages such as Python or Java, here is the same example but using a strong typed programming language, the other side of the coin.

Python’s example:

num = 5  # num is an integer
str = "10" # str is a string

result = str(num) + str # Error: TypeError: 'int' object is not callable
print(result)

In Python, variables don’t have explicit type declarations, but they still have a specific type associated with them. In this example, num it is an integer, and str is a string. When you try to concatenate num and str, Python raises a TypeError because it doesn’t perform implicit type coercion. To avoid the error, you need to explicitly convert num to a string using the str() function.

Java’s example:

int num = 5;  // num is an int
String str = "10"; // str is a String

String result = Integer.toString(num) + str; // No implicit type coercion
System.out.println(result);

In Java, variables have explicit types that are declared when they are defined. In this example, num is an int, and str is a String. When concatenating num and str, Java doesn’t perform implicit type coercion. Instead, you must explicitly convert num to a string using the Integer.toString() method to ensure the concatenation works as expected.

Interview Questions

What does it mean that Javascript is a High-Level Language?

It means that it provides a higher level of abstraction, so it is designed to be easier for humans to read, write, and understand.

What does it mean that Javascript is a dynamic and weakly typed programming language?

It means that Javascript can hold values with different data types in the same variable during runtime [dynamic], and it can perform operations between different data types without explicitly converting them before [weakly].

P.S. If you are a native Spanish speaker or learning this beautiful language, you can check out the twin post that will be in Spanish. The same concept and explanations but in another language 😉 every week.

--

--

Code & Cozy

Welcome to Code & Cozy, a tech blog where programming meets warmth and inspiration. Join us on a journey of learning and growth in the world of web development.