Lesson not found
Lesson Progress: 0%
let firstName: string = "Alice";
console.log(firstName);
let age: number = 25;
console.log(age);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
: string or : number.string holds words, sentences, or any text, and it always goes inside quotation marks, like "Alice".number holds math values, like 25, and you do not put quotes around them.firstName is labeled as a string and remembers the name "Alice".age is labeled as a number and remembers the value 25.console.log() shows whatever is inside the parentheses on the screen so you can see the value.let isLoggedIn: boolean = true;
console.log(isLoggedIn);
let isComplete: boolean = false;
console.log(isComplete);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
boolean is a type that can only hold one of two things: true or false.isLoggedIn is set to true, meaning the user is currently signed in.isComplete is set to false, meaning the task is not finished yet.let email: string | null = null;
console.log(email);
let middleName: string | undefined = undefined;
console.log(middleName);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
null means "this is intentionally empty." You set it to null on purpose.undefined means "this has not been given a value yet." The box is there, but nothing has been placed inside.| is called a union type and means "or." string | null means the value can either be a string or null.email is set to null because the user did not provide an email address yet.middleName is set to undefined because no middle name was given.null and undefined helps your code clearly show when something is missing on purpose versus not yet set.function sayHello(firstName: string): void {
console.log("Hello, " + firstName);
}
sayHello("Alice");Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
void type means "this function does not return anything." It does its job but does not give a value back.sayHello prints a greeting to the screen, but there is noreturn keyword, so nothing is sent back.: void after the parentheses tells TypeScript that the function promises not to return any value.sayHello("Alice"), the function runs, prints the message, and then finishes quietly.void used often for functions that only display information, save data, or perform an action without giving anything back.let userInput: unknown = "hello";
console.log(userInput);
let data: any = 42;
data = "anything";
console.log(data);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
unknown is a safe type for when you do not know what kind of data you will get. You must check it before using it.userInput is labeled unknown and starts as a string, but TypeScript will not let you use it freely until you prove what it is.any is the most flexible type. It turns off type checking completely, so you can put any kind of value inside.data starts as the number 42, then changes to the string "anything", and TypeScript does not complain.any is easy to use, it removes the safety that TypeScript gives you, so try to use it only when you really need to.function fail(message: string): never {
throw new Error(message);
}Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
never type means a function never finishes normally. It either throws an error or runs forever.fail uses throw new Error(message), which immediately stops the program.never.never as saying "this function is not expected to complete successfully."never type is less common than the others, but it is useful for error handling and functions that must always stop.Test Incomplete