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
EasyCreate a Dart program that uses the type test operators (is and is!) to check variable types:
- Create a variable
numberwith the value42using type inference (var) - Create a variable
decimalwith the value3.14using type inference - Create a variable
textwith the value"Hello"using type inference - Create a variable
activewith the valuetrueusing type inference - Use the
isoperator to check if each variable is of its correct type (int, double, String, bool) - Use the
is!operator to check if each variable is NOT of an incorrect type - 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 typeis!- 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}'); // trueTry 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}");
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number