JavaScript isset equivalent

Programmers who come to JavaScript from languages such PHP, may be wondering how to check if the specific variable is set.

We can do this with many ways, and one of the easiest is to check with typeof if the variable’s type is not undefined.

Example:

Check whether the variable is defined or not.

<script>
    var x = 10;
    // var y doesn't exist!

    if(typeof x !== 'undefined') {
        console.log(x);
    } else {
        console.log("x doesn't exist");
    }

    if(typeof y !== 'undefined') {
        console.log(y);
    } else {
        console.log("y doesn't exist");
    }
</script>

Output:

10
y doesn't exist