liveBook · Manning

String

As explained in Essential TypeScript 5, Third Edition

String is a built-in JavaScript function and constructor that converts values to strings and can create String objects.

String is a built-in JavaScript function and constructor that converts values to strings and can create String objects. In everyday code, string values represent text and can be written with single quotes, double quotes, or template strings. section 3.2 section 3.3.1

JavaScript assigns types to values rather than variables. A variable can therefore hold a string and later hold a number, while typeof reports the type of its current value. section 3.3.1 section 7.2

A string can change an operation

let hatPrice = 100; let bootsPrice = "100"; if (hatPrice == bootsPrice) { console.log("Prices are the same"); } let totalPrice = hatPrice + bootsPrice; console.log(`Total Price: ${totalPrice}`); // Prices are the same // Total Price: 100100 section 3.2

Inspecting the current value type

let myVar; console.log(`${myVar} = ${typeof myVar}`); myVar = 12; console.log(`${myVar} = ${typeof myVar}`); myVar = "Hello"; console.log(`${myVar} = ${typeof myVar}`); myVar = true; console.log(`${myVar} = ${typeof myVar}`); section 7.2

Listing 3.6 Adding statements in the index.ts file in the primer folder
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);

if (hatPrice == bootsPrice) {
    console.log("Prices are the same");    
} else {
    console.log("Prices are different");    
}

let totalPrice = hatPrice + bootsPrice;
console.log(`Total Price: ${totalPrice}`);

Compare the numeric and string prices, then observe how the string changes addition from arithmetic into concatenation. section 3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines string values and demonstrates syntax, typeof inspection, dynamic reassignment, and coercion, but does not detail String constructor internals.
Examples
Uses quoted names, template literals, mixed price values, typeof output, and a function receiving string, number, and boolean arguments.
What it assumes
Assumes familiarity with JavaScript variables, assignment, expressions, and console output while teaching string syntax and dynamic value types.
What you can do after
Diagnose unexpected output by inspecting string values with typeof and checking whether mixed operations concatenate or coerce.
How it connects
Connects strings with JavaScript types, typeof, template strings, equality comparison, and type coercion.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.