Quirks of JS for Python dev 🐍

Quirks of JS for Python dev 🐍

¡

4 min read

🤝 Connect with me on LinkedIn, Visit my website.

  1. 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
    
  2. 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
    
  3. 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)
    
  4. 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)
    
  5. 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
    
  6. null and undefined are two distinct entities in JavaScript.
    Example:

     let x = null;  
     let y;  
     console.log(x, y); // Outputs: null undefined
    
  7. Be aware of the scoping differences between var, let, and const.
    Example:

     if (true) {  
         var a = 1;  
         let b = 2;  
     }  
     console.log(a); // Outputs: 1  
     console.log(b); // Error: b is not defined
    
  8. 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
    
  9. 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)
    
  10. For example, console.log(1 == 1) and console.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
    
  11. == (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
    
  12. 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");

🤝 Connect with me on LinkedIn, Visit my website.

Subscribe to our newsletter

Read articles from Aditya Adarsh directly inside your inbox. Subscribe to the newsletter, and don't miss out.