Bird
Raised Fist0
Typescriptprogramming~10 mins

String type behavior in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String type behavior
Declare string variable
↓
Assign string value
↓
Use string methods or operations
↓
Get new string or output
↓
Strings are immutable, original unchanged
This flow shows how a string variable is declared, assigned, used with methods, and how strings remain unchanged because they are immutable.
Execution Sample
Typescript
let greeting: string = "Hello";
let shout = greeting.toUpperCase();
console.log(shout);
console.log(greeting);
This code converts a string to uppercase and shows that the original string stays the same.
Execution Table
StepActionVariableValueOutput
1Declare greetinggreeting"Hello"
2Call toUpperCase()shout"HELLO"
3Print shoutshout"HELLO"HELLO
4Print greetinggreeting"Hello"Hello
5EndExecution complete
💡 All steps executed; strings are immutable so greeting remains "Hello"
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
greetingundefined"Hello""Hello""Hello""Hello""Hello"
shoutundefinedundefined"HELLO""HELLO""HELLO""HELLO"
Key Moments - 2 Insights
Why does greeting stay "Hello" after calling toUpperCase()?
Because strings are immutable in TypeScript, toUpperCase() returns a new string without changing the original greeting variable, as shown in steps 2 and 4 of the execution_table.
What does toUpperCase() do exactly?
It creates and returns a new string with all letters in uppercase, without modifying the original string variable, demonstrated by the new value assigned to shout in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of greeting after step 3?
Aundefined
B"Hello"
C"HELLO"
D"hello"
💡 Hint
Check the 'Variable' and 'Value' columns for greeting at step 3 in the execution_table.
At which step is the new uppercase string stored in shout?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Variable' columns in the execution_table to see when shout gets its value.
If we changed greeting to "hello" (all lowercase), what would shout be after step 2?
A"HELLO"
B"hello"
C"Hello"
Dundefined
💡 Hint
toUpperCase() converts all letters to uppercase regardless of original casing, see step 2 behavior.
Concept Snapshot
String type behavior in TypeScript:
- Strings are immutable (cannot change once created).
- Methods like toUpperCase() return new strings.
- Original string variables stay unchanged.
- Use string methods to get new transformed strings.
- Always assign method results to new variables or overwrite.
Full Transcript
This example shows how string variables behave in TypeScript. We declare a string variable greeting with value "Hello". When we call greeting.toUpperCase(), it returns a new string "HELLO" which we store in shout. Printing shout shows "HELLO". Printing greeting still shows "Hello" because strings are immutable and the original string does not change. This teaches that string methods return new strings and do not modify the original variable.

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

  1. 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.
  2. 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.
  3. Final Answer:

    let name: string = "Alice"; -> Option A
  4. 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

  1. Step 1: Identify template string syntax

    Template strings use backticks ` and allow embedding variables with ${variable}.
  2. 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.
  3. Final Answer:

    let greeting = `Hello, ${name}!`; -> Option A
  4. 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?
const text: string = "Hello World";
console.log(text.length);
medium
A. 10
B. 11
C. 12
D. Error

Solution

  1. Step 1: Understand string length property

    The length property returns the number of characters in the string, including spaces.
  2. Step 2: Count characters in "Hello World"

    "Hello World" has 11 characters: 5 letters + 1 space + 5 letters.
  3. Final Answer:

    11 -> Option B
  4. Quick Check:

    "Hello World" length = 11 [OK]
Hint: Count all characters including spaces for length [OK]
Common Mistakes:
  • Ignoring spaces in length count
  • Confusing length with last index
  • Expecting syntax error
4. Identify the error in this TypeScript code snippet:
let message: string = 'Hello';
message.toUppercase();
medium
A. Strings do not have toUpperCase method
B. No error, outputs 'HELLO'
C. Method name should be toUpperCase() with capital C
D. Missing semicolon after string declaration

Solution

  1. Step 1: Check method name spelling

    The correct method to convert string to uppercase is toUpperCase() with capital 'C'.
  2. Step 2: Identify error cause

    Using toUppercase() (lowercase 'c') causes a runtime error because the method does not exist.
  3. Final Answer:

    Method name should be toUpperCase() with capital C -> Option C
  4. Quick Check:

    Correct method spelling = Method name should be toUpperCase() with capital C [OK]
Hint: Remember uppercase C in toUpperCase() method [OK]
Common Mistakes:
  • Using wrong case in method names
  • Assuming strings lack case methods
  • Ignoring runtime errors
5. Given the following code, what does this code check?
const name: string = "Anna";
const reversed = name.split("").reverse().join("");
console.log(reversed === name);
hard
A. If the string length is even
B. If the string is empty
C. If the string contains only letters
D. If the string is a palindrome (same forwards and backwards)

Solution

  1. Step 1: Understand string reversal steps

    The code splits the string into characters, reverses the array, then joins back to a string.
  2. Step 2: Compare reversed string with original

    It checks if the reversed string equals the original string, which means the string reads the same forwards and backwards.
  3. Final Answer:

    If the string is a palindrome (same forwards and backwards) -> Option D
  4. Quick Check:

    Reversed equals original means palindrome = If the string is a palindrome (same forwards and backwards) [OK]
Hint: Reversing and comparing checks palindrome [OK]
Common Mistakes:
  • Confusing palindrome with empty string
  • Thinking it checks string length or letters
  • Missing split-reverse-join logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard