var

An older keyword for declaring a variable that belongs to the whole function, not just the block it's in.

What is var in JavaScript?

var — An older keyword for declaring a variable that belongs to the whole function, not just the block it's in. Modern code uses let and const instead.

function checkout(items) {
    if (items.length > 0) {
        var discount = 5;
    }
    return discount; // still visible here: var ignores the if block
}

Level: Novice · More JavaScript terms at this level

Updated

In other subjects