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.

FAQ: JavaScript Strings

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript 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 javascript strings in this JavaScript JavaScript lesson (JavaScript Strings).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.