Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære String Type | Core TypeScript Types
TypeScript Foundations

bookString Type

Strings are one of the most fundamental data types in TypeScript. You use the string type to represent sequences of characters, such as words, sentences, or any textual data. In TypeScript, you can define string values using single quotes ('...'), double quotes ("..."), or template literals (`...`). Each style is functionally equivalent for storing text, but template literals provide additional features that make them especially useful in many situations.

123456789
// Declaring string variables with different syntaxes let singleQuoteString: string = 'Hello, TypeScript!'; let doubleQuoteString: string = "Welcome to the string type."; let templateLiteralString: string = `This is a template literal.`; // Using template literals for string interpolation let userName: string = 'Sam'; let greeting: string = `Hello, ${userName}!`; console.log(greeting); // Output: Hello, Sam!
copy

Template literals are enclosed by backticks instead of quotes. They allow you to embed expressions directly within the string using ${...}. This process is called string interpolation. Interpolation makes it easy to construct dynamic strings based on variable values or expressions, without the need for manual concatenation. TypeScript enforces type safety with strings. If you attempt to assign a non-string value to a variable declared as string, you will receive a compile-time error. This helps you catch mistakes early and ensures that your variables always hold the type of data you expect.

question mark

Which of the following is the correct way to declare a string variable?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 5.56

bookString Type

Stryg for at vise menuen

Strings are one of the most fundamental data types in TypeScript. You use the string type to represent sequences of characters, such as words, sentences, or any textual data. In TypeScript, you can define string values using single quotes ('...'), double quotes ("..."), or template literals (`...`). Each style is functionally equivalent for storing text, but template literals provide additional features that make them especially useful in many situations.

123456789
// Declaring string variables with different syntaxes let singleQuoteString: string = 'Hello, TypeScript!'; let doubleQuoteString: string = "Welcome to the string type."; let templateLiteralString: string = `This is a template literal.`; // Using template literals for string interpolation let userName: string = 'Sam'; let greeting: string = `Hello, ${userName}!`; console.log(greeting); // Output: Hello, Sam!
copy

Template literals are enclosed by backticks instead of quotes. They allow you to embed expressions directly within the string using ${...}. This process is called string interpolation. Interpolation makes it easy to construct dynamic strings based on variable values or expressions, without the need for manual concatenation. TypeScript enforces type safety with strings. If you attempt to assign a non-string value to a variable declared as string, you will receive a compile-time error. This helps you catch mistakes early and ensures that your variables always hold the type of data you expect.

question mark

Which of the following is the correct way to declare a string variable?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt