Skip to content

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

  1. Create a new TypeScript file

Number Operations

  1. Declare a variable named number of type number and assign it the value 10.
  2. Increment the number variable using ++.
  3. Add 5 to the number variable using +=.
  4. Subtract 2 from the number variable.
  5. Multiply the number variable by 3.
  6. Divide the number variable by 2.
  7. Print number using console.log.

String Operations

  1. Declare a variable named text of type string and assign it the value “hello”.
  2. Print text using console.log.
  3. Declare another variable named upperText of type string and assign it the value of text in uppercase.
  4. Print upperText using console.log.
  5. Declare a variable named textLength of type number and assign it the length of text.
  6. Print textLength using console.log.

Template Strings

  1. Declare a variable named firstName of type string and assign it your first name.
  2. Declare a variable named age of type number and assign it your age.
  3. Using a template string, create a variable named greeting that reads “My name is [firstName] and I am [age] years old”.
  4. Print greeting using console.log.

Boolean Operations

  1. Declare a variable named isStudent of type boolean and set its value to true.
  2. Print isStudent using console.log.
  3. Declare a variable named hasLicense of type boolean and set its value to false.
  4. Print hasLicense using console.log.

Simple Math

  1. Declare two variables named a and b of type number with values 15 and 7.
  2. Declare a variable named sum and assign it the sum of a and b.
  3. Declare a variable named difference and assign it the difference between a and b.
  4. Declare a variable named product and assign it the product of a and b.
  5. Declare a variable named quotient and assign it the division of a by b.
  6. Print all results using console.log.

Working with const and let

  1. Declare a constant named PI of type number and assign it the value 3.14159.
  2. Print PI using console.log.
  3. Try to change the value of PI (this should cause an error - comment it out after trying).
  4. Declare a variable with let named counter of type number and assign it the value 0.
  5. Increment counter three times.
  6. Print counter using console.log.

Date Variable

  1. Declare a variable named today of type Date and set its value to the current date and time.
  2. Print today using console.log.
  3. Declare a variable named year of type number and assign it the current year from today.
  4. Print year using console.log.

Challenge: Number Precision

  1. Declare a variable named price of type number and assign it the value 19.99.
  2. Declare a variable named quantity of type number and assign it the value 3.
  3. Declare a variable named total and calculate the total price.
  4. Print the result with 2 decimal places using toFixed(2).

Challenge: String Manipulation

  1. Declare a variable named fullName of type string and assign it “John Doe”.
  2. Split the fullName into an array of words.
  3. Print the first word (first name) using console.log.
  4. Print the second word (last name) using console.log.

Challenge: Type Checking

  1. Declare a variable named value and assign it the number 42.
  2. Print the type of value using typeof and console.log.
  3. Reassign value to the string “forty-two”.
  4. Print the type of value again using typeof and console.log.
  5. 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'