TypeScript Tutorial

TypeScript Strings

string holds text. Concatenate, measure length, and slice — with a type the compiler can check.

Declare a string

A string holds a sequence of characters. Write the type after the name, then a value in double quotes. You do not include a header. tsc already knows string.

Example

const word: string = "TypeScript";
console.log(word);

Try this at /typescript/try with Try it in TypeScript. The TypeScript editor compiles with tsc.

Concatenate with +

+ joins strings. If one side is a string, the other side is converted to text and glued on. Two number operands still add. Mix a string with a number and you concatenate, so"n=" + 3 is n=3.

Example

const first: string = "Ada";
const last: string = "Lovelace";
const full: string = first + " " + last;
console.log(first);
console.log(full);
console.log(first + " wrote programs");

length and the first character

length is a property, not a call. It is how many characters are in the string. Index[0] is the first character, type string of length 1. Indexes start at zero, so the last character is at length - 1 when the string is not empty.

Example

const word: string = "TypeScript";
console.log(word.length);
console.log(word[0]);
console.log(word[1]);

Do not read word[0] when the string is empty. You get undefined at run time. Checklength first, or only index strings you just filled with text.

slice copies a piece

slice(start, end) returns a new string from index start up to, but not including,end. Omit end and it runs to the end. The original string does not change.

Example

const name: string = "StudyGrid";
const tag: string = name.slice(0, 5);
console.log(name);
console.log("first five: " + tag);
console.log(name.slice(5));

Change 5 to 4 and compile again. The slice shortens. C++ usedsubstr for this idea; TypeScript uses slice.

Input is already a string

readLine() at /typescript/try returns a string, including spaces on that line. You do not need a separate “read a word” call. Type a full name in the stdin box, then compile.

NeedTool
Join text+
Count characters.length
Take a piece.slice(start, end)
Read a line in the editorreadLine()

Quotes

Double quotes and single quotes both make strings: "A" and 'A' are the same type. TypeScript has no separate char type. Next: math functions on the global Math object.

FAQ: TypeScript Strings

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript strings examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript strings in this TypeScript TypeScript lesson (TypeScript Strings).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.