Skip to main content

Operators in Dart

Operators are used to perform mathematical and logical operations on the variables. Each operation in dart uses a symbol called the operator to denote the type of operation it performs. Before learning operators in the dart, you must understand the following things.

Operands : It represents the data. Operator It represents how the operands will be processed to produce a value.

Note

Suppose the given expression is 2 + 3. Here 2 and 3 are operands, and + is the operator.

Types Of Operators​

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Logical Operators
  • Type Test Operators

Arithmetic Operators​

Arithmetic operators are the most common types of operators. They perform operations like addition, subtraction, multiplication, division, etc.

Operator SymbolOperation NameDescription
+AdditionFor adding two operands
-SubtractionFor subtracting two operands
-exprUnaryMinus For reversing the sign of the expression
*MultiplicationFor multiplying two operands
/DivisionFor dividing two operands and give output in double
~/DivisionFor dividing two operands and give output in integer
%ModulusRemainder After Integer Division
++IncrementIncrease Value By 1. For E.g a++;
--DecrementDecrease Value By 1. For E.g a–;

Let’s look at how to perform arithmetic calculations in dart.

Increment and Decrement Operators​

With increment and decrement operators, you can increase and decrease values. If ++ is used at the beginning, then it is a prefix. If it is used at last, then it is postfix.

Operator SymbolOperation NameDescription
++varPre IncrementIncrease Value By 1. var = var + 1 Expression value is var+1
--varPre DecrementDecrease Value By 1. var = var - 1 Expression value is var-1
var++Post IncrementIncrease Value By 1. var = var + 1 Expression value is var
var--Post DecrementDecrease Value By 1. var = var - 1 Expression value is var
Note

++var increases the value of operands, whereas var++ return the actual value of operands before the increment.

Assignment Operators​

It is used to assign some values to variables. Here, we are assigning 24 to the age variable.


int age = 24;
Operator TypeDescription
=Assign a value to a variable
+=Adds a value to a variable
-=Reduces a value to a variable
*=Multiply value to a variable
/=Divided value by a variable
Note

To assign only if the assign-to variable is null use ?? operator.

Relational Operators​

Relational operators are also called comparison operators. They are used to make a comparison.

Operator SymbolOperation NameDescription
>Greater thanUsed to check which operand is bigger and gives result as boolean
<Less thanUsed to check which operand is smaller and gives result as boolean
>=Greater than or equal toUsed to check which operand is bigger or equal and gives result as boolean
<=Less than or equal toUsed to check which operand is smaller or equal and gives result as boolean
==Equal toUsed to check operands are equal to each other and gives result as boolean
!=Not equal toUsed to check operand are not equal to each other and gives result as boolean

Logical Operators​

It is used to compare values.

Operator TypeDescription
&&This is β€˜and’, return true if all conditions are true
2 equalThis is β€˜or’. Return true if one of the conditions is true
!This is ’not’. return false if the result is true and vice versa

Type Test Operators​

In dart, type test operators are useful for checking types at runtime.

Operator SymbolOperation NameDescription
isisGives boolean value true if the object has a specific type
is!is notGives boolean value false if the object has a specific type