JavaScript String Object and Primitive Strings

JavaScript provides a built-in String type for working with text. In normal application code, use string primitives such as "Welcome"; JavaScript still lets you call String methods on those primitive values.

const message = "Welcome to plus2net.com";
console.log(message.length);
console.log(message.toUpperCase());

Avoid creating wrapper objects with new String() unless you specifically need to study wrapper-object behavior.

String Primitive Values Top ↑

String literals are primitive values and are the normal way to store text.

const site = "plus2net.com";
console.log(typeof site); // "string"

Creating a String Object with new String() Top ↑

The original page created a String object like this. It still works, but the result is an object rather than a primitive string.

const myString = new String("Welcome to plus2net.com");
console.log(typeof myString); // "object"
Recommended: prefer primitive strings. Wrapper objects can behave unexpectedly in Boolean checks and strict comparisons.

Using String() for Type Conversion Top ↑

Calling String() without new is useful for converting another value to text.

const count = 42;
const text = String(count);
console.log(text, typeof text); // "42" "string"

JavaScript Strings Are Immutable Top ↑

Methods return new strings. They do not edit the original string value.

const original = "javascript";
const upper = original.toUpperCase();
console.log(original); // javascript
console.log(upper);    // JAVASCRIPT

String Properties and Common Methods Top ↑

The original page demonstrated the length property. It counts UTF-16 code units.

const myString = "Welcome to plus2net.com";
console.log(myString.length); // 23

Other frequently used methods include toUpperCase(), toLowerCase(), includes(), indexOf(), slice(), substring(), replace(), trim(), startsWith() and endsWith().

Primitive String vs String Object Comparison Top ↑

const primitive = "hello";
const objectValue = new String("hello");
console.log(primitive === objectValue); // false
console.log(primitive === objectValue.valueOf()); // true

Legacy String HTML Wrapper Methods Top ↑

The original tutorial demonstrated methods such as bold(), fontsize(), blink(), fixed(), fontcolor(), italics(), link(), strike() and sub(). These are legacy HTML wrapper methods and should not be used to build modern interfaces.

const text = "Hello";
console.log(text.bold()); // legacy: returns an HTML wrapper string

Use semantic HTML/DOM APIs and CSS instead. The legacy strike method example remains available for maintaining old code.

Extracting a Substring Top ↑

The original String-object page also demonstrated substring extraction. Modern primitive strings support the same String prototype methods.

const myString = "Welcome to plus2net.com";
console.log(myString.substring(11, 19)); // plus2net

See substring() and slice() for the full extraction rules.

Strings, UTF-16 and Unicode Top ↑

JavaScript strings are indexed as UTF-16 code units. Basic Latin characters normally occupy one code unit, while some Unicode symbols use a pair. For those cases, codePointAt(), String.fromCodePoint(), spread syntax, or Array.from() can be more appropriate than processing one code unit at a time.

Common String Object Mistakes Top ↑

  • Using new String() for ordinary text values.
  • Expecting String methods to mutate the original string.
  • Using deprecated HTML wrapper methods for presentation.
  • Assuming strict equality treats a wrapper object and primitive string as identical.
  • Assuming length always counts user-perceived Unicode characters.

String Reference Uppercase Strings




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