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 Answer
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 - 2The
varkeyword is being deprecated in 0.4.20, see [github.com/ethereum/solidity/releases/tag/v0.4.20](release notes).Jaryl– Jaryl2018-02-19 10:11:33 +00:00Commented Feb 19, 2018 at 10:11 - 1@Jaryl, that link is broken0TTT0– 0TTT02018-02-26 14:06:32 +00:00Commented Feb 26, 2018 at 14:06
- 3Oops, the correct link is here github.com/ethereum/solidity/releases/tag/v0.4.20Jaryl– Jaryl2018-02-26 16:05:10 +00:00Commented Feb 26, 2018 at 16:05