T005 Simple Variables and Types
In this assignment, you’ll practice the basics of TypeScript variables and types. You’ll work with numbers, strings, booleans, and dates to understand how TypeScript’s static typing helps you write safer code.
Setup
- Create a new TypeScript file
Number Operations
- Declare a variable named
numberof typenumberand assign it the value 10. - Increment the
numbervariable using++. - Add 5 to the
numbervariable using+=. - Subtract 2 from the
numbervariable. - Multiply the
numbervariable by 3. - Divide the
numbervariable by 2. - Print
numberusingconsole.log.
String Operations
- Declare a variable named
textof typestringand assign it the value “hello”. - Print
textusingconsole.log. - Declare another variable named
upperTextof typestringand assign it the value oftextin uppercase. - Print
upperTextusingconsole.log. - Declare a variable named
textLengthof typenumberand assign it the length oftext. - Print
textLengthusingconsole.log.
Template Strings
- Declare a variable named
firstNameof typestringand assign it your first name. - Declare a variable named
ageof typenumberand assign it your age. - Using a template string, create a variable named
greetingthat reads “My name is [firstName] and I am [age] years old”. - Print
greetingusingconsole.log.
Boolean Operations
- Declare a variable named
isStudentof typebooleanand set its value to true. - Print
isStudentusingconsole.log. - Declare a variable named
hasLicenseof typebooleanand set its value to false. - Print
hasLicenseusingconsole.log.
Simple Math
- Declare two variables named
aandbof typenumberwith values 15 and 7. - Declare a variable named
sumand assign it the sum ofaandb. - Declare a variable named
differenceand assign it the difference betweenaandb. - Declare a variable named
productand assign it the product ofaandb. - Declare a variable named
quotientand assign it the division ofabyb. - Print all results using
console.log.
Working with const and let
- Declare a constant named
PIof typenumberand assign it the value 3.14159. - Print
PIusingconsole.log. - Try to change the value of
PI(this should cause an error - comment it out after trying). - Declare a variable with
letnamedcounterof typenumberand assign it the value 0. - Increment
counterthree times. - Print
counterusingconsole.log.
Date Variable
- Declare a variable named
todayof typeDateand set its value to the current date and time. - Print
todayusingconsole.log. - Declare a variable named
yearof typenumberand assign it the current year fromtoday. - Print
yearusingconsole.log.
Challenge: Number Precision
- Declare a variable named
priceof typenumberand assign it the value 19.99. - Declare a variable named
quantityof typenumberand assign it the value 3. - Declare a variable named
totaland calculate the total price. - Print the result with 2 decimal places using
toFixed(2).
Challenge: String Manipulation
- Declare a variable named
fullNameof typestringand assign it “John Doe”. - Split the
fullNameinto an array of words. - Print the first word (first name) using
console.log. - Print the second word (last name) using
console.log.
Challenge: Type Checking
- Declare a variable named
valueand assign it the number 42. - Print the type of
valueusingtypeofandconsole.log. - Reassign
valueto the string “forty-two”. - Print the type of
valueagain usingtypeofandconsole.log. - What happens if you try to assign a type annotation to
value? Try it!
Once you’ve completed the assignment, you can compile the TypeScript file using the TypeScript compiler (tsc) to generate the corresponding JavaScript file.
Solution
let number: number = 10;
number++; // Now 11
number += 5; // Now 16
number -= 2; // Now 14
number *= 3; // Now 42
number /= 2; // Now 21
console.log(number); // Output: 21
let text: string = "hello";
console.log(text); // Output: hello
let upperText: string = text.toUpperCase();
console.log(upperText); // Output: HELLO
let textLength: number = text.length;
console.log(textLength); // Output: 5
let firstName: string = "Alice";
let age: number = 25;
let greeting: string = `My name is ${firstName} and I am ${age} years old`;
console.log(greeting); // Output: My name is Alice and I am 25 years old
let isStudent: boolean = true;
console.log(isStudent); // Output: true
let hasLicense: boolean = false;
console.log(hasLicense); // Output: false
let a: number = 15;
let b: number = 7;
let sum: number = a + b;
let difference: number = a - b;
let product: number = a * b;
let quotient: number = a / b;
console.log(`Sum: ${sum}`); // Output: Sum: 22
console.log(`Difference: ${difference}`); // Output: Difference: 8
console.log(`Product: ${product}`); // Output: Product: 105
console.log(`Quotient: ${quotient}`); // Output: Quotient: 2.142857...
const PI: number = 3.14159;
console.log(PI); // Output: 3.14159
// PI = 3.14; // Error: Cannot assign to 'PI' because it is a constant
let counter: number = 0;
counter++;
counter++;
counter++;
console.log(counter); // Output: 3
let today: Date = new Date();
console.log(today); // Output: Current date and time
let year: number = today.getFullYear();
console.log(year); // Output: Current year (e.g., 2025)
let price: number = 19.99;
let quantity: number = 3;
let total: number = price * quantity;
console.log(`Total: $${total.toFixed(2)}`); // Output: Total: $59.97
let fullName: string = "John Doe";
let nameParts: string[] = fullName.split(" ");
console.log(nameParts[0]); // Output: John
console.log(nameParts[1]); // Output: Doe
let value = 42;
console.log(typeof value); // Output: number
value = "forty-two" as any; // Need type assertion to change type
console.log(typeof value); // Output: string
// If you declare with type annotation:
let typedValue: number = 42;
console.log(typeof typedValue); // Output: number
// typedValue = "forty-two"; // Error: Type 'string' is not assignable to type 'number'