Strings hold text in programs. Understanding how they behave helps you work with words and sentences easily.
When you want to store a name or message.
When you need to join words together.
When you want to check if a word contains certain letters.
When you want to change text to uppercase or lowercase.
When you want to find the length of a sentence.
Syntax
Typescript
let variableName: string = "your text here";
Strings are written inside double quotes "", single quotes '', or backticks `` for template strings.
TypeScript uses string type to represent text values.
Examples
Simple string assignment using double quotes.
Typescript
let greeting: string = "Hello";
String can also use single quotes.
Typescript
let name: string = 'Alice';
Template string using backticks allows embedding variables.
Typescript
let message: string = `Hi, ${name}!`;
An empty string has no characters but is still a string.
Typescript
let empty: string = "";
Sample Program
This program shows how to create strings, join them, find length, convert to uppercase, and check if a substring exists.
Typescript
let firstName: string = "John";
let lastName: string = "Doe";
let fullName: string = `${firstName} ${lastName}`;
console.log("Full name:", fullName);
console.log("Length of full name:", fullName.length);
console.log("Uppercase:", fullName.toUpperCase());
console.log("Contains 'Doe'?:", fullName.includes("Doe"));
OutputSuccess
Important Notes
Strings in TypeScript are immutable, meaning you cannot change a character directly.
Use string methods like toUpperCase(), includes(), and length to work with strings.
Summary
Strings store text and use the string type in TypeScript.
You can create strings with quotes or backticks for templates.
Use built-in methods to check, change case, or find length of strings.
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?