📖 Scenario: Imagine you are working on a simple program that handles names of fruits. You want to learn how strings work in TypeScript by creating and manipulating string variables.
🎯 Goal: You will create a string variable, add another string to it, and then display the final combined string.
📋 What You'll Learn
Create a string variable with a specific value
Create another string variable to add
Combine the two strings using the + operator
Print the combined string
💡 Why This Matters
🌍 Real World
Handling and combining text data is common in many programs, like showing names, messages, or labels.
💼 Career
Understanding string behavior is essential for any programming job that involves user input, display, or text processing.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called fruit and set it to the value "Apple".
Typescript
Hint
Use let to declare a variable and set its type to string.
2
Create another string variable
Create another string variable called moreFruit and set it to the value " Banana" (note the space before Banana).
Typescript
Hint
Remember to include the space before Banana so the words separate when combined.
3
Combine the two strings
Create a new string variable called allFruits that combines fruit and moreFruit using the + operator.
Typescript
Hint
Use the + operator to join two strings.
4
Print the combined string
Use console.log to print the value of allFruits.
Typescript
Hint
Use console.log(allFruits); to show the combined string in the console.
Practice
(1/5)
1. Which of the following is the correct way to declare a string variable in TypeScript?
easy
A. let name: string = "Alice";
B. let name = 123;
C. let name: number = "Alice";
D. let name: boolean = true;
Solution
Step 1: Understand TypeScript string type declaration
In TypeScript, to declare a string variable, use let variableName: string = value; where value is text in quotes.
Step 2: Check each option's type and value
let name: string = "Alice"; uses string type and assigns a text value "Alice" correctly. Others assign wrong types or values.
Final Answer:
let name: string = "Alice"; -> Option A
Quick Check:
Correct string declaration = let name: string = "Alice"; [OK]
Hint: String variables use : string and quotes [OK]
Common Mistakes:
Assigning number or boolean to string type
Missing type annotation when required
Using wrong quotes for strings
2. Which of the following is the correct syntax to create a template string in TypeScript?
easy
A. let greeting = `Hello, ${name}!`;
B. let greeting = Hello, ${name}!;
C. let greeting = "Hello, ${name}!";
D. let greeting = 'Hello, ${name}!';
Solution
Step 1: Identify template string syntax
Template strings use backticks ` and allow embedding variables with ${variable}.
Step 2: Check each option's quotes and variable embedding
let greeting = `Hello, ${name}!`; correctly uses backticks and ${name} syntax. Options A and C use single or double quotes which do not support variable embedding. let greeting = Hello, ${name}!; misses quotes entirely.
Final Answer:
let greeting = `Hello, ${name}!`; -> Option A
Quick Check:
Template strings use backticks = let greeting = `Hello, ${name}!`; [OK]
Hint: Use backticks ` for template strings with ${} [OK]
Common Mistakes:
Using single or double quotes for templates
Forgetting backticks
Not wrapping string in quotes
3. What is the output of the following TypeScript code?