7

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?

1
  • Where did you see that? Commented Jun 15, 2017 at 6:24

1 Answer 1

11

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 
3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.