Basic Types: str, num, boolean
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 6 of 73.
TypeScript has three fundamental primitive types that form the building blocks of most applications: string, number, and boolean. These types allow you to enforce what kind of data your variables can hold.
The string type is used for text data. You can declare a string variable like this:
let productName: string = "Laptop";The number type handles all numeric values in TypeScript - both integers and decimals:
let price: number = 999.99;The boolean type represents true/false values, perfect for flags and conditions:
let inStock: boolean = true;Challenge
EasyCreate three separate variables with explicit type annotations and appropriate values:
Declare a variable named productName with type string and assign it the value "Gaming Mouse".
Declare a variable named price with type number and assign it the value 49.99.
Declare a variable named inStock with type boolean and assign it the value true.
Print all three variables to the console on separate lines in the order they were declared.
Try it yourself
// TODO: Write your code here
// Declare the three variables with explicit type annotations and values
// Then print them to the consoleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples