JavaScript Strings: Methods and Properties

A JavaScript string is a sequence of text characters used for values such as names, messages, URLs, labels, and user input. Strings can be written with single quotes, double quotes, or template literals.

const name = "plus2net";
const message = `Welcome to ${name}`;
console.log(message); // Welcome to plus2net

JavaScript strings are immutable: methods return new strings rather than changing the original text in place.

Creating JavaScript Strings Top ↑

Single and double quotes create ordinary string literals. Template literals use backticks and are especially useful for interpolation and multiline text.

const first = 'JavaScript';
const second = "String";
const topic = `${first} ${second}`;
console.log(topic);

String Primitive vs String Object Top ↑

Most code should use string primitives such as "hello". JavaScript temporarily exposes String methods on primitive values, so you normally do not need new String(). See the JavaScript String object tutorial for the difference.

String Length and Character Access Top ↑

Use length to count UTF-16 code units. Use charAt() or bracket access for positions, and charCodeAt() when you need a UTF-16 code-unit value.

const text = "JavaScript";
console.log(text.length);    // 10
console.log(text.charAt(0)); // J
console.log(text[0]);       // J

Use indexOf() when you need a numeric position. Use includes() for a simple true/false test. Use search() when a regular expression is useful.

Extracting Part of a String Top ↑

slice() and substring() extract ranges. The older substr() page is retained for legacy-code understanding, but new code should generally prefer slice() or substring().

Changing Case, Replacing, Reversing and Combining Text Top ↑

Strings themselves do not change; methods return new values. Explore toUpperCase(), toLowerCase(), replace(), concat(), and the string reverse examples.

Unicode and UTF-16 Top ↑

JavaScript string indexes and length operate on UTF-16 code units. Some Unicode characters can therefore occupy two string positions. The String.fromCharCode() and charCodeAt() tutorials explain code units; modern Unicode code-point handling can use codePointAt() and String.fromCodePoint().

Legacy String-to-HTML Methods Top ↑

Older JavaScript exposes methods such as bold() and link(). These HTML wrapper methods are legacy features; modern pages should create semantic HTML with markup or DOM APIs and use CSS for presentation. The old tutorials remain useful when maintaining historical code.

The older escape() / unescape() topic is also retained for legacy reference. For URL components, modern JavaScript normally uses encodeURIComponent() and decodeURIComponent().

JavaScript String Tutorial Reference Top ↑

TutorialWhat it teaches
String objectPrimitive strings, String objects, methods, immutability and legacy wrappers.
lengthString length, indexes, empty strings and Unicode considerations.
charAt()Read a UTF-16 code unit at a zero-based position.
charCodeAt()Get a UTF-16 code-unit number.
String.fromCharCode()Create text from UTF-16 code units.
concat()Combine strings and compare with + and template literals.
indexOf() / lastIndexOf()Find the position of text.
search()Search with strings or regular expressions.
slice()Extract text with start/end indexes, including negative indexes.
substring()Extract text between start and end indexes.
substr()Understand legacy start-plus-length extraction.
replace()Find and replace text.
toUpperCase() / toLowerCase()Change letter case.
Email user/domain exampleCombine index and extraction methods on an email-like value.
onblur text case exampleApply string methods after a form field loses focus.

Common JavaScript String Mistakes Top ↑

  • Expecting a string method to mutate the original string.
  • Forgetting that string indexes start at 0.
  • Assuming length always equals the number of user-perceived Unicode characters.
  • Using new String() when a primitive string is sufficient.
  • Using legacy HTML wrapper or escaping methods in new code.



Subscribe to our YouTube Channel here



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer