Syntax Overview
Dart syntax is similar to C, Java, and JavaScript. Understanding the basic syntax rules helps you write valid Dart code and avoid errors.
Statements & Expressions
A statement is a complete instruction that performs an action. Every statement in Dart ends with a semicolon (;).
int age = 25; // declaration statement
print(age); // expression statement
age = age + 1; // assignment statement
An expression is a piece of code that evaluates to a value:
5 + 3 // evaluates to 8
age > 18 // evaluates to true or false
'Hello' // evaluates to the string "Hello"
Case Sensitivity
Dart is case-sensitive. name, Name, and NAME are three different identifiers.
String name = 'Alice';
// String Name = 'Bob'; ← different variable!
print(name); // Alice
Whitespace & Indentation
Dart ignores extra whitespace and blank lines. However, consistent indentation (2 or 4 spaces) makes code readable. The dart format tool formats code automatically.
// Both are valid but the second is more readable:
void main(){print('Hi');}
void main() {
print('Hi');
}
Dart Keywords
Keywords are reserved words with special meaning. You cannot use them as variable names:
abstract as assert async await
break case catch class const
continue default do else enum
extends false final finally for
if import in is late
new null return static super
switch this throw true try
var void while with
Full Example
void main() {
// Variables and types
String language = 'Dart';
int version = 3;
bool isAwesome = true;
// Output with string interpolation
print('$language version $version is awesome: $isAwesome');
// Simple arithmetic
int a = 10;
int b = 3;
print('Sum: $${a + b}');
print('Product: $${a * b}');
}
Output:
Dart version 3 is awesome: true
Sum: 13
Product: 30
⚠️ Common Mistakes
- Forgetting the semicolon at the end of a statement.
- Using a keyword as a variable name (e.g.,
var class = 5;).
- Mixing up capitalization (e.g., calling
Print() instead of print()).
🧠 Quiz
1. What ends a statement in Dart?
- A) Newline
- B) Semicolon ✅
- C) Colon
- D) Period
2. Is Dart case-sensitive?
- A) No
- B) Yes ✅
- C) Only for keywords
- D) Only for class names
Summary
Dart syntax is C-style with semicolons ending statements. It is case-sensitive. Whitespace is ignored but indentation improves readability. Dart keywords are reserved and cannot be used as identifiers.