đ¤ Connect with me on LinkedIn, Visit my website.
JavaScript is not strongly typed.
Though Python is also not strongly typed, it provides type hints. JavaScript lacks built-in type checking, although tools like TypeScript (superset of JS) can be used to introduce it.
Example:let x = 42; x = "Now I'm a string"; // No error in JavaScript
Python Example:
def add(a: int, b: int) -> int: return a + b print(add(1, "2")) # TypeError: '+' not supported between instances of 'int' and 'str' # Error when using pydentic lib (optional) for type checking during input # pass
Functions can accept more arguments than required, and these arguments can be of any datatype (e.g., Object, Array, or a single variable) without throwing an error. Even if a function doesnât require an argument, passing one wonât result in an error.
Example:function greet(name) { console.log("Hello, " + name); } greet("Aditya", 25); // No error, "Aditya" is used, 25 is ignored
Integers (
int
) can be added to strings (str
) and vice versa without throwing an error. Be cautious!
Example:let result = 5 + "5"; console.log(result); // Outputs: "55" (string concatenation)
The
sort
function converts everything to a string by default, even if you pass an array of integers or floats.
Example:let numbers = [10, 2, 5]; numbers.sort(); console.log(numbers); // Outputs: [10, 2, 5] (as strings)
Deleting a value from an array works differently than in Python. It leaves an empty placeholder, and the arrayâs length remains unchanged.
Example:let arr = [1, 2, 3]; delete arr[1]; console.log(arr); // Outputs: [1, empty, 3] console.log(arr.length); // Outputs: 3
null
andundefined
are two distinct entities in JavaScript.
Example:let x = null; let y; console.log(x, y); // Outputs: null undefined
Be aware of the scoping differences between
var
,let
, andconst
.
Example:if (true) { var a = 1; let b = 2; } console.log(a); // Outputs: 1 console.log(b); // Error: b is not defined
JavaScript execution is single-threaded in nature. JavaScriptâs power lies in callbacks and asynchronous programming, which can be confusing. Many modern APIs, functions, and libraries have built-in callbacks, which may feel unintuitive for Python developers.
Example:console.log("Start"); setTimeout(() => console.log("Callback"), 1000); console.log("End"); // Outputs: Start, End, Callback
Debugging in JavaScript often requires more effort as it doesnât throw explicit errors but instead produces incorrect values or outputs.
Example:let result = "5" * 2; console.log(result); // Outputs: 10 (implicit type conversion)
For example,
console.log(1 == 1)
andconsole.log(1 == [1])
both return true in JavaScript, whereas Python would return False for the second case.
Example:console.log(1 == 1); // Outputs: true console.log(1 == [1]); // Outputs: true
==
(Double Equals) vs===
(Triple Equals): Example:true === 1 // false (boolean vs. number) |Loose Equality null === undefined // false (different types) |Loose Equality 1 == [1] // true |Loose Equality 5 === 5 // true (both value and type are the same) |Strict Equality 5 === '5' // false (different types: number vs. string) |Strict Equality
Be cautious with semicolons (;). While JavaScript doesnât rely on indentation for block separation and automatic semicolon insertion (ASI) is available, relying on it can lead to unexpected behavior. Always try to include semicolons explicitly.
Example -```javascript // Incorrect let a = 10 let b = 20 let c = a + b (a + b).toString()
console.log(c) // Output: 30 (expected correct output)
// line (a + b).toString() is interpreted as part of the previous line // because of ASI like let c = a + b(a + b).toString();
// Correct let a = 10; let b = 20; let c = a + b; (a + b).toString(); // Does nothing but doesn't affect c
console.log(c); // Output: 30 (correct output)
13. **JavaScript execution is single-threaded in nature. JavaScriptâs power lies in callbacks and asynchronous programming, which can be confusing. Many modern APIs, functions, and libraries have built-in callbacks, which may feel unintuitive for Python developers. Async programming in JavaScript doesnât mean multi-threading but is a way to asynchronously run the code without waiting for the current operation to complete. This is particularly useful for database queries, I/O operations, API calls, etc.** Example
```javascript
console.log("Start");
// Simulates an API call
setTimeout(() => console.log("Fetching data..."), 2000);
console.log("End");
Subscribe to our newsletter
Read articles from Aditya Adarsh directly inside your inbox. Subscribe to the newsletter, and don't miss out.