ProgramGuru

The string Type

Learn The string Type

A string stores text. TypeScript checks string operations, while JavaScript performs them at runtime.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind the string type.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Single quotes, double quotes and backticks delimit strings. Template literals can interpolate expressions using ${...}. String values are immutable; operations such as toUpperCase produce new strings. Indexing and length use JavaScript UTF-16 code units, which can differ from user-perceived characters.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const language: string = "TypeScript";
 const message = `Learn ${language}`;
 console.log(message);
 console.log(language.toUpperCase());

Verified output

Learn TypeScript
TYPESCRIPT

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The template inserts TypeScript into the message. toUpperCase returns a new uppercase string; it does not alter language.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not mix up the source quotation marks with the string contents or assume string.length always counts visible characters.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Replace language with your favorite language. Print the first three characters with slice(0, 3).

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What does a template literal use for interpolation?
Choose one answer. You can try again.

Answer and explanation

${expression} inside backticks.

Template strings are JavaScript runtime syntax.

Review the key idea

The string Type: Single quotes, double quotes and backticks delimit strings.

Template strings are JavaScript runtime syntax.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: The boolean Type →