Quotes
Use single or double quotes consistently. Escape the same quote type inside the string.
Strings store text. They look simple, but real string work includes quotes, escaping, length, indexing, normalization and safe output.
const title = "JavaScript"; const message = "Learn " + title; console.log(message.length); console.log(message[0]);
Strings, Numbers & Math
A JavaScript string is a sequence of characters. You can create strings with double quotes, single quotes or backticks. Backticks create template literals, which are covered in the next lesson.
Strings are primitive values and immutable. That means a method such as toUpperCase() returns a new string. It does not change the original value.
In real projects, strings show up everywhere: labels, headings, search text, form values, API fields, paths, messages and generated markup. Treat them as data, not as random pieces of text.
Use single or double quotes consistently. Escape the same quote type inside the string.
string.length returns the number of UTF-16 code units, not always the number of visible characters.
string[0] reads a character-like unit by position. Indexes start at 0.
String methods return new values. Assign the result if you need to keep it.
Examples
const rawInput = " JavaScript "; const normalized = rawInput.trim().toLowerCase(); if (normalized === "javascript") { console.log("Match found"); }
const rawInput = " JavaScript "; if (rawInput === "javascript") { console.log("This will not run."); }
Syntax reference
This is the practical reference part of the lesson. Each example has one job, a stable anchor and a small assignment, so the page works both as a course and as a developer reference when you need the syntax later.
Use quotes for normal text values.
const label = "Save changes"; const status = "ready"; console.log(label);
Use a backslash when the quote belongs inside the text.
const message = "Press \"Run\" to preview the code."; console.log(message);
Useful for counters and simple validation.
const input = "JavaScript"; if (input.length > 20) { console.log("Text is too long."); }
Store the returned value when you transform text.
const original = "javascript"; const normalized = original.toUpperCase(); console.log(original); // "javascript" console.log(normalized); // "JAVASCRIPT"
Code patterns
These small examples are designed for scanning. Use them when you need the syntax quickly, then read the surrounding notes when you want the deeper reason behind the pattern.
Use quotes for normal text values.
const label = "Save changes"; const status = "ready"; console.log(label);
Use a backslash when the quote belongs inside the text.
const message = "Press \"Run\" to preview the code."; console.log(message);
Useful for counters and simple validation.
const input = "JavaScript"; if (input.length > 20) { console.log("Text is too long."); }
Store the returned value when you transform text.
const original = "javascript"; const normalized = original.toUpperCase(); console.log(original); // "javascript" console.log(normalized); // "JAVASCRIPT"
Rules that matter
Strings and numbers are simple until they cross a boundary: form input, API data, generated output, rounding, formatting or search. Normalize, validate and format deliberately.
Consistency makes scanning easier and reduces escaping noise.
Trim spaces and choose the right case before comparing text.
Methods return new strings. The original value stays the same.
Do not turn untrusted strings into HTML with innerHTML.
An empty string can be a valid value or a missing value. Decide deliberately.
Visible characters and string length are not always the same thing.
Production thinking
Many bugs are text bugs: invisible spaces, wrong case, unsafe output or a value that looks empty but is not handled correctly.
Text is what assistive technology reads. String logic should produce clear labels, messages and status updates.
Production string handling needs normalization, escaping, validation and predictable formatting at system boundaries.
Headings, titles, metadata and links are strings. Broken string logic can create poor or duplicated visible content.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.