5 Things You don't know about JavaScript
    • news
    • -
    • 5 Things You don't know about JavaScript

    5 Things You don't know about JavaScript

    JavaScript is a beautiful language. It has its secrets, which many people do not know, and which can surprise. Here is a list of several such secrets.

    1. NaN is a number

    Did you know that NaN is that a number? NaNso not a number, when you expand the abbreviation, it’s actually a number.

    console.log(typeof NaN); //number

    What’s more, NaN it is not even equal to itself and is not equal to any other object. If you have to check if anything occurs as NaNthen you must use isNaN().

    NaN === NaN // false
    isNaN(NaN) //true

    2. Void Operator

    In JavaScript, there is an operator void. You are probably wondering what such an operator can do in JS. So it takes whatever expression comes after it, runs it, and always returns undefined. Sound useless? I think it does, or at least I thought it did.

    void {}; // undefined
    void "any text at all"; // undefined
    void (() => {}); // undefined
    let a = 0;
    void a; // undefined

    Do we even need something like this? Surprisingly, as much as possible. It is used to define undefined. In fact, I didn’t even know that undefined you can define.

    (() => {
    const undefined = "foo";
    console.log(undefined, typeof undefined); // "foo", "string"
    console.log(void 0, typeof void 0); // undefined, "undefined"

    Because undefined if it is not a keyword, then you can use it as a variable name to override the global value in the range.

    3. IIFE, i.e. Immediately Invoked Function Expression

    I don’t think it’s that secret. IIFE is actually a function that can be called immediately after its definition. IIFE is usually wrapped in parentheses followed by parentheses to call a defined function.

    () => {
    // …

    When a function has a returned type, or when it is assigned to a variable, we don’t need to do that.

    void function () {
    // …
    const result = function () {
    // …

    4. Operator in

    One of the less commonly used operators in JS is the operator in. Its purpose is to check whether the object contains the appropriate property.

    let obj = { x: 1, y: 2, z: 3 };
    "x" in obj; // true
    "a" in obj; // false

    But why do we need an operator inwhen we can check it with help object[property]? Because it gives us the convenience of checking whether any property is present, even if it has any values false. In such cases, we usually use:

    typeof obj[prop] === "undefined"

    Instead of the above, we can also use:

    "prop" in obj

    5. Null is an object

    We all assumed that null it informs us that there is no object. However, let’s try to see what null ma type:

    console.log(typeof null); //object

    Come on. null it is also not an instance of an object, because it indicates that there is no value. So:

    console.log(null instanceof Object); // false


    The above are just a few js concepts that I had no idea about. I hope you can use them. Let us know in the comments whether you knew about them. Or maybe you know others?

    You can read the original English text here.



    1A Sportyvna sq, Kyiv, Ukraine 01023

    1608 Queen St, Wilmington, NC, 28401