Dart vs Javascript

Oct 5, 2021
4 min read

Dart was first used in Google for web servers and mobile applications. Its journey began around 2011, but it was not well known outside Google at that time. Dart is compiled in the same way as other programming languages, and the standalone Dart SDK is shipped with the Dart VM. Dart has a package manager called the pub. When Google officially announced Flutter in 2017, the language Dart garnered more attention in the industry. Dart bumped up even more after the 2018 Google I/O conference. Mobile developers are increasingly turning to Flutter and if you've mastered OOPS, that would be your plus point for learning Dart.

In addition to types, Dart is also a stricter language. When you work in JavaScript, you deal with a "truthy" value in conditional.

Now if we take the same code in Dart, you will know that the condition must have static type bool.

Your code will turn like this:

if (strName.length > 0)

And another preferred way is:

if (strName.isNotEmpty)

One more point related to this is that no triple equals (===) will be found in Dart.

Print

In JavaScript, you use the console.log method to write a JavaScript object, a JavaScript string, or primitive values such as integers and floats.

But in Dart you would use print:

print('My name is $Jonathan');

And whenever you want to access properties on variables and call functions, all you have to do is wrap them in curly braces.

print('length is ${strName.length}');

Map

We can get a new list from the map, in which all the elements are transformed.

var mappedCountries = country.map((country) => ‘$country’).toList();

print(mappedCountries); // => ['Albania', ‘Algeria’, ‘Andorra’]

Or

map.forEach((k, v) => list.add(Employee(k, v)));

print(list);

Map in Dart is a dictionary-like data type that contains key-value pairs. There is no restriction in the data type of Map, but you have to keep in mind that keys must be unique inside the Map data type. The list is similar to an array and is a collection of items in an ordered way. You can specify the length of a list if you want. And you can even make it growable if you want.

contains()

Many times you want to check if an item is in the list or not then there is a contain() method.

var numberArray = [1, 3, 2, 5, 4];

print(numberArray.contains(5)); // => true

Function Parameters

When it comes to function parameters, you will find a big difference between JavaScript and Dart. Dart can certainly take your time but you are gonna find its implementation powerful.

Either you can pass parameters in setPosition or you can pass by name also.

getFullName('Jonathan', 'Hitchens');

by name:

getFullName(firstName: ''Jonathan'', lastName: ''Hitchens'');

In the case of the constructor, this.fieldName dictates that the value passed is to be assigned to the property.

Contact(this.LastName);

Handling Arrays

The array is almost the same, but we have mentioned the slight difference here.

Firstly, instead of push; add is used when items are needed to be added inside the array. Dart has helper methods, which are "first" and "firstWhere", which as the name suggests will give you the first item of the array. If the match is not found, an error will occur and you will return the value in "orElse" you need to deal with the issue.

Final and Const

The most important thing to understand here is that final describes the variable, and the const describes the value. Once the final variable is set, you can change the value to whatever it is pointing to, but the value of the const is frozen and you cannot change it.

Another great tip you'll find when working with the Flutter analysis_options file is that you need to use const constructors wherever possible. And as soon as you enable the framework to cache widgets, your app's performance will increase significantly.

Fat Arrow

FAT arrow is used for the single-line function.

It will twist like this:

myFunction: () => true,

Doing so makes your readability much prettier.

Explore Generics

Dart generic guarantees object types, and thus benefits Flutter. Coding a class or a function in such a way that it works with a range of data types, and not just one is called the generic approach. With generics, you can do more basic type checking at compile-time and thus type safety will be built into your code. Collections hold the same type of information and type-safety is authorized. Generics allow you to create reusable classes, methods, and functions with various information types. When you build Flutter applications with Dart, you'll use generics for collections, futures, and streams, which are actually "centre library highlights".

Collection generics means that each component inside the collection will be of the normal kind.

List myList;

Map myMap;

This code is identical to the code in the snippet given below:

You will need this code when you are working with a wide range of types. Dart Analyzer will work with the type specified inside angle brackets and won't let you make a mistake.

List<String> myList;

Dart vs JavaScript: Type safety

JavaScript supports both: dynamic and duck typing. An application developer can type any code and JavaScript will not notify about errors while coding. You will only get errors at runtime, so JavaScript is not a type-safe language. Dart on the other hand is a compiled language and catches errors during compilation.

Last Words

If this is your first exposure to Dart, I hope it is insightful and will give you a quick start. If you already know Javascript, your pickup will be much faster. Dart also supports object-oriented concepts such as interfaces, abstract classes, static typing, and more. One thing is certain that the Dart has taken off and can handle a really difficult task incredibly quickly. Also, it does not require specific hardware configuration or architecture.