Menu
Coddy logo textTech

Type Test Operators

Part of the Fundamentals section of Coddy's Dart journey — lesson 26 of 94.

Dart's type test operators check variable types at runtime: is verifies if a variable matches a type, while is! checks if it doesn't.

void main() {
  var number = 42;
  var isInteger = number is int;
  
  print('Is $number an integer? $isInteger');
}
void main() {
  var price = 9.99;
  var isNotInteger = price is! int;
  
  print('Is $price not an integer? $isNotInteger');
}
void main() {
  var value1 = 'Hello';
  var value2 = 42;
  var value3 = true;
  
  print('$value1 is a String: ${value1 is String}');
  print('$value2 is a double: ${value2 is double}');
  print('$value3 is a bool: ${value3 is bool}');
}
challenge icon

Challenge

Easy

Create a Dart program that uses the type test operators (is and is!) to check variable types:

  1. Create a variable number with the value 42 using type inference (var)
  2. Create a variable decimal with the value 3.14 using type inference
  3. Create a variable text with the value "Hello" using type inference
  4. Create a variable active with the value true using type inference
  5. Use the is operator to check if each variable is of its correct type (int, double, String, bool)
  6. Use the is! operator to check if each variable is NOT of an incorrect type
  7. Print the results with the exact format shown in the expected output

Your output must match this exact format:

number is int: true
number is! String: true
decimal is double: true
decimal is! int: true
text is String: true
text is! bool: true
active is bool: true
active is! double: true

Cheat sheet

Dart's type test operators check variable types at runtime:

  • is - verifies if a variable matches a type
  • is! - checks if a variable doesn't match a type
var number = 42;
var isInteger = number is int;        // true
var isNotString = number is! String;  // true

print('Is $number an integer? $isInteger');
print('Is $number not a string? $isNotString');

You can check multiple types:

var value1 = 'Hello';
var value2 = 42;
var value3 = true;

print('$value1 is a String: ${value1 is String}');  // true
print('$value2 is a double: ${value2 is double}');  // false
print('$value3 is a bool: ${value3 is bool}');      // true

Try it yourself

void main() {
  // Create variables using type inference
  var number = ?;
  var decimal = ?;
  var text = ?;
  var active = ?;
  
  // Check types with 'is' operator and print results
  print("number is int: ${number ? int}");
  print("number is! String: ${number ? String}");
  
  print("decimal is double: ${decimal ? double}");
  print("decimal is! int: ${decimal ? int}");
  
  print("text is String: ${text ? String}");
  print("text is! bool: ${text ? bool}");
  
  print("active is bool: ${active ? bool}");
  print("active is! double: ${active ? double}");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals