📖 Scenario: Imagine you are working with text data in different programming languages. Each language treats strings a bit differently. In this project, you will explore how strings behave in TypeScript by creating and manipulating strings step-by-step.
🎯 Goal: You will create a string, add a helper variable to check its length, manipulate the string by slicing it, and finally print the result to see how strings work in TypeScript.
📋 What You'll Learn
Create a string variable with exact text
Create a helper variable to store string length
Use string slicing to get a substring
Print the final substring
💡 Why This Matters
🌍 Real World
Working with strings is common in many programs, like processing user input, displaying messages, or handling text data.
💼 Career
Understanding how strings work in TypeScript helps you manipulate text data effectively, a key skill for frontend and backend development.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called greeting and set it to the exact value "Hello, TypeScript!".
DSA Typescript
Hint
Use const to declare the string variable and assign the exact text.
2
Add a helper variable for string length
Create a variable called lengthOfGreeting and set it to the length of the string greeting using greeting.length.
DSA Typescript
Hint
Use the .length property to get the number of characters in the string.
3
Slice the string to get a substring
Create a variable called shortGreeting and set it to the first 5 characters of greeting using greeting.slice(0, 5).
DSA Typescript
Hint
Use the slice method to get a part of the string from index 0 up to but not including index 5.
4
Print the substring
Use console.log to print the value of shortGreeting.
DSA Typescript
Hint
Use console.log(shortGreeting); to display the substring.
Practice
(1/5)
1. In TypeScript, what is the type of a string when you use single quotes like 'hello' compared to double quotes like "hello"?
easy
A. Both are of type string and behave the same
B. Single quotes create a character, double quotes create a string
C. Single quotes create a string, double quotes create a character
D. Single quotes are invalid syntax in TypeScript
Solution
Step 1: Understand string literals in TypeScript
TypeScript treats both single and double quotes as string delimiters, so both create string values.
Step 2: Confirm no character type in TypeScript
Unlike some languages, TypeScript does not have a separate character type; single quotes do not create characters.
Final Answer:
Both are of type string and behave the same -> Option A
Quick Check:
Single and double quotes create strings = A [OK]
Hint: Single and double quotes both create strings in TypeScript [OK]
Common Mistakes:
Thinking single quotes create characters
Confusing TypeScript with languages like C
Assuming single quotes are invalid
2. Which of the following is the correct way to declare a multi-line string in TypeScript?
easy
A. Using double quotes with + operator: const s = "line1" + "line2";
B. Using single quotes with \n: const s = 'line1\nline2';
C. Using backticks: const s = `line1\nline2`;
D. Using triple quotes: const s = '''line1\nline2''';
Solution
Step 1: Identify multi-line string syntax in TypeScript
TypeScript supports template literals using backticks (`) which allow multi-line strings directly.
Step 2: Check other options for correctness
Single and double quotes require explicit \n for new lines; triple quotes are invalid in TypeScript.
Final Answer:
Using backticks: const s = `line1\nline2`; -> Option C
Quick Check:
Backticks allow multi-line strings = A [OK]
Hint: Use backticks for multi-line strings in TypeScript [OK]
Common Mistakes:
Using triple quotes like Python
Expecting single or double quotes to support multi-line directly
Forgetting to escape newlines in single/double quotes
3. What will be the output of the following TypeScript code?
const s = `Hello\nWorld`;
console.log(s.length);
medium
A. Error
B. 12
C. 10
D. 11
Solution
Step 1: Understand the string content
The string is a template literal with an actual newline character between Hello and World, not the two-character escape sequence.
Step 2: Count characters including the newline
Characters: H(1) e(2) l(3) l(4) o(5) \n is a single newline character (counts as 1), W(6) o(7) r(8) l(9) d(10). So length is 11.
Final Answer:
11 -> Option D
Quick Check:
Template literals interpret \n as newline character = A [OK]
Hint: Template literals interpret \n as a single newline character [OK]
Common Mistakes:
Assuming \n is two characters in length
Counting only visible characters
Expecting runtime error due to escape
4. Identify the error in this TypeScript code snippet:
const greeting = 'Hello;
console.log(greeting);
medium
A. Missing closing single quote for the string
B. Incorrect use of backticks instead of single quotes
C. Missing semicolon after console.log statement
D. No error, code runs fine
Solution
Step 1: Check string syntax
The string starts with a single quote but does not have a closing single quote before the newline.