Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS String as Objects

JavaScript Strings as Objects

Typically, JavaScript strings are primitive values that are generated from literals.

let x = “John”;

However, strings can also be created as objects using the new keyword.

let y = new String(“John”);

Example

let x = “John”;
let y = new String(“John”);

Avoid creating String objects.

Using the new keyword complicates the code and can reduce execution speed. Additionally, String objects may lead to unexpected results.

When using the == operator, x and y are considered equal.

let x = “John”;
let y = new String(“John”);

When using the === operator, x and y are not considered equal.

let x = “John”;
let y = new String(“John”);
Be aware of the distinction between (x == y) and (x === y).

Is (x == y) true or false?

let x = new String(“John”);
let y = new String(“John”);

Is (x === y) true or false?

let x = new String(“John”);
let y = new String(“John”);