On this page

Block scope var declaration

This warning category is spelled [block-scope-var-declaration] by qmllint.

A variable has been declared with var inside a block scope

What happened?

A JavaScript variable has been declared with var inside of a block scope, for example in the body of a while loop. While it might look like the variable is only usable inside that scope, it can actually be accessed outside of it, as it is hoisted to the scope of the innermost function or QML binding.

Why is this bad?

It can be confusing to track the scope of such a variable, and they might accidentally shadow other variables.

Example

import QtQuick

Item {
    id: root
    property int count: 0
    function countItems() {
        let sum = 0
        {
            var count = root.children.length
            sum += count
        }
        // ... more additions to sum
        count = sum // modifies the function local variable, not root.count
    }
}

To fix this warning, use let or const instead, or declare the variable directly in the function scope.

import QtQuick

Item {
    id: root
    property int count: 0
    function countItems() {
        let sum = 0
        {
            let count = root.children.length
            sum += count
        }
        // ... more additions to sum
        count = sum // modifies the function local variable, not root.count
    }
}

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.