- Qt 6.11
- Qt Qml
- Qt Qml Tooling
- qmllint
- QML Lint Warning and Errors
- Var used before declaration
- Qt 6.11.2 Reference Documentation

Use proper function ◦ Do not use void in expressions

# Var used before declaration

This warning category is spelled `[var-used-before-declaration]` by qmllint.

## Variable is used here before its declaration

### What happened?

You used a variable before you declared it.

### Why is this bad?

This makes the code harder to read. Variables declared with `let` or `const` will error out at runtime.

### Example

     import QtQuick

     Item {
         function f() {
             x = 42; // x is used before its declaration
             let x;
         }

         Component.onCompleted: f()
     }

To fix this warning, move the declaration before the usage:

     import QtQuick

     Item {
         function f() {
             let x;
             x = 42;
         }

         Component.onCompleted: f()
     }

See also JavaScript Expressions in QML Documents.

Use proper function ◦ Do not use void in expressions