4

I see it in some contract examples, but it doesn't look like it has the exact same purpose as regular JavaScript. Can it be used to initialize variables in a specific scope without declaring a type?

8

From the docs:

Type Deduction

For convenience, it is not always necessary to explicitly specify the type of a variable, the compiler automatically infers it from the type of the first expression that is assigned to the variable:

uint24 x = 0x123;
var y = x;

Here, the type of y will be uint24. Using var is not possible for function parameters or return parameters.

This is also a must for Destructuring Assignments

function f() public pure returns (uint, bool, uint) {
        return (7, true, 2);
}
var (x,y,z) = f(); //Multi-Return from Fx

(x,y) = (y,x) //Swap Values

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.