var

noun

say it: “VAR” /vɑːr/

JavaScript's oldest keyword for making a variable; the variable belongs to the whole function, not the block it's in, and let and const have mostly replaced it.

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
}

In a sentence

  • “The old tutorial used var for the loop counter, so the counter was still there after the loop ended.”

Level: Novice · More JavaScript terms at this level

Updated

In other subjects