DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for var vs dynamic in Dart
Nitish Kumar Singh
Nitish Kumar Singh

Posted on • Updated on

var vs dynamic in Dart

In Dart and JavaScript we have var keyword for the variable declaration but both are not same.

There is the dynamic keyword for variable declaration in Dart which is equivalent to JavaScript var keyword.

Dart var keyword is very much similar to javascript var keyword but not the exact same. Most of Dart developer uses Dart var as JS var which is not correct. Let's understand this by some examples.

I will give 4 different examples.

Example 1

In JavaScript and Dart, we can store any kind of data in a variable which has been defined using var keyword.

Dart

// This is valid Example this will work
void main() {
Β     var x = 'Maths pi';
Β     var y = 3.14;

Β     print(x); // Maths pi
Β     print(y); // 3.14
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

// This is valid Example this will work
Β var x = 'Maths pi';
Β var y = 3.14;

Β console.log(x); // Maths pi
Β console.log(y); // 3.14
Enter fullscreen mode Exit fullscreen mode

Example 2

In JavaScript and Dart, we can store different kind of data in the same variable.

Dart

// This is valid Example this will work
void main() {
Β    var xΒ ;
    x = 'Math pi';
Β    print(x); // Maths pi

    x = 3.14;
Β    print(x); // 3.14
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

// This is valid Example this will work
Β var xΒ ;
 x = 'Math pi';
Β console.log(x); // Maths pi

 x = 3.14;
Β console.log(x); // 3.14
Enter fullscreen mode Exit fullscreen mode

Example 3

Now I am going to mix Example 1 and Example 2
Now I am going to initialize the variable and trying to store different data type in the same variable. This is going to give me some error because this is not valid in Dart but its valid in JavaScript.

This is the place where var behaves differently in Dart

Dart

// This is invalid Example this will work
void main() {

Β     var x = 'Math pi';
Β     print(x);  // Maths pi


     x = 3.14;  // Compilation failed error because
Β                // you are trying to assign double to String variable.
Β     print(x);
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

// This is valid Example this will work
Β var xΒ ;
 x = 'Math pi';
Β console.log(x); // Maths pi
 x = 3.14;
Β console.log(x); // 3.14
Enter fullscreen mode Exit fullscreen mode

Example 4

If we had used dynamic instead of var in Dart code we haven't got error because dart dynamic is same as JavaScript var.

Dart

// This is invalid Example this will not work
void main() {
Β     var x = 'Math pi';
Β     print(x); // Maths pi
     x = 3.14;Β 
Β     print(x); // expected 3.14
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

// This is valid Example this will work
Β var xΒ ;
 x = 'Math pi';
Β console.log(x); // Maths pi
 x = 3.14;
Β console.log(x); // 3.14
Enter fullscreen mode Exit fullscreen mode

This is my first post here. If you like it let me so that I can be motivated to post here

Top comments (6)

Collapse
 
dance2die profile image
Sung M. Kim

Thanks for the post, Nitish πŸ‘

I've never used Dart and the post was very easy to follow.
How I understood about var in Dart is that, Dart keeps track of initial variable type while dynamic.

And shouldn't the example 4 use dynamic keyword?

// This is invalid Example this will work
void main() {
     dynamic x = 'Math pi';
     print(x); // Maths pi
     x = 3.14; 
     print(x); // 3.14
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
slaveofcode profile image
Aditya Kresna

Awesome!

Collapse
 
klanmiko profile image
Kaelan Mikowicz

Cool, so var can be either dynamic or another type depending on how it is initialized.

Collapse
 
graphicbeacon profile image
Jermaine Oppong

Thanks for sharing Nitish! Keep them coming.

Collapse
 
josefatef profile image
josefatef

Thanks

Collapse
 
ilgeion86 profile image
Vy

Very usefull post for understanding. Thnx !

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›