JavaScript Tutorial
JavaScript Strings
Text lives in quotes. Length, indexing, and concatenation are the first tools.
Text in quotes
A string is a sequence of characters. Write it in double quotes "Hello" or single quotes'Hello'. Both are the same type. Pick one style and stay with it in a file. Backticks are a third form — template literals — covered two chapters from here.
Use the other quote when the text itself contains a quote: "Mina's book" or'He said "go".'. An empty string is "" — a valid string with length 0.
length
text.length is the number of characters. It is a property, not a function: no parentheses. For an empty string it is 0. Length counts every character, including spaces and punctuation.
Example
const city = "Lisbon";
console.log(city);
console.log(city.length);
const empty = "";
console.log(empty.length);
const line = "Hello, Mina";
console.log(line.length);
Click Try it in JavaScript under the example. That opens /javascript/try,. Length prints in the console.
Indexing
Characters are numbered from 0. The first character is text[0]. The last istext[text.length - 1]. An index past the end is undefined, not an error.
Example
const city = "Lisbon";
console.log(city[0]);
console.log(city[1]);
console.log(city[city.length - 1]);
console.log(city[99]);
L, i, n, then undefined. There is no separate character type: each index is a string of length 1.
You cannot assign to an index. city[0] = "M" does not change the string. Strings do not mutate. The next chapter builds new strings with methods instead.
Concatenation
+ glues strings together. If one side is a number, JavaScript turns it into text first. That is useful for a label plus a count. It is also how "Total: " + 3 + 2 becomes "Total: 32"instead of 5 — concatenation starts as soon as a string appears, then continues left to right.
Example
const first = "Mina";
const last = "Costa";
const full = first + " " + last;
console.log(full);
const n = 3;
console.log("You have " + n + " items.");
console.log("Total: " + 3 + 2);
console.log("Total: " + (3 + 2));
Parentheses force the addition: "Total: " + (3 + 2) is "Total: 5". Template literals in a later chapter embed values without this + chain.
Escape characters
A backslash starts an escape. \' and \" put a quote inside a string that uses the same quote. \n is a new line. \\ is a real backslash. You need these in single- and double-quoted strings. Backtick templates can span lines without \n.
Example
console.log("She said \"ready\".");
console.log('Mina\'s laptop');
console.log("Line one\nLine two");
console.log("C:\\Users\\Mina");
Comparing strings
=== tests exact equality, including case: "Mina" === "mina" is false.< and > compare in Unicode order, which for plain English is roughly dictionary order, with uppercase before lowercase. Trim and normalize case before you compare user input:input.value.trim().toLowerCase() === "yes".
Strings do not mutate
Methods and + always produce a new string. The original stays as it was unless you assign the new one back to the name: city = city + "!". That assignment replaces the binding. It does not edit characters in place. The next chapter is the method set: slice, trim,toUpperCase, includes, and replace.
Next: string methods — they return a copy. The original string stays put.