ECMAScript Strings Tutorial

View more Tutorials:

1- ECMAScript String

In  ECMAScript, String is a special data type, because it is used frequently in a program, therefore, it is required to have performance and flexibility. That's reason why the String is both objective and primitive.

String Literal

You can create a  string literal. The  string literal is stored in a stack, requiring less storage space, and cheaper to manipulate.
let aStringLiteral = "Hello World";
string literals are contained in a Common Pool. When you declare two String Literal variables with the same content, they will point to the same address stored on the pool. 
let a = "Tom";
let b = "Jerry";
let c = "Jerry";

console.log(a); // Tom
console.log(b); // Jerry
console.log(c); // Jerry

// a, b: Same address?
console.log( a === b); // false

// b, c: Same address?
console.log( b === c); // true

String Object

You can create a string through the  constructor of the  String class. In this case, you will receive a  String object.
let myStringObj = new String(value);
  • value: A value to convert to string
let a = new String("Hello World");
console.log(a); // [String: 'Hello World']

let b = new String(true);
console.log(b); // [String: 'true']

let c = new String(100.20);
console.log(c); // [String: '100.2']
The new operator always creates a new entity on the Heap memory, which explains why you have to spend more money using String Objects
When you assign a new value that is variable aa to variable bb. The variable bb will point to the address on memory to which the variable aa is pointing. There will not be an entity created on memory in this case.  
let aa = new String("Tom");

let bb = aa;

// aa, bb: Same address?
var sameAddress = aa === bb;

console.log( sameAddress); // true

String object vs String Literal

String Literals are stored on Stack while String Objects are created and stored on the  Heap memory. It requires complex memory management and storage space. So you should use String Literal instead of String Object at any possible time. 


The  typeof(string_object) operator will return  'object' string  while typeof(string_literal) will return the 'string' string
let myStringObj = new String("Tom");
console.log(myStringObj); // [String: 'Tom']
console.log( typeof(myStringObj) ); // object

let myStringLiteral =  "Jerry";
console.log(myStringLiteral); // Jerry
console.log( typeof(myStringLiteral) ); // string

Function - String(something)

The  String(something) function convert something to  String Literal.
// String Object:
let s = new String("Hello");
console.log(s); // [String: 'Hello']

// String(value) function:
// Convert String Object to String Literal.
let s2 = String(s);
console.log(s2); // Hello

// A Number Object.
let number = new Number(300.20);
let n2 = String(number);

console.log( n2 ); // 300.2

2- String is Imutable

In the  ECMAScript, the  String data type is imutable, which means that any action you do on the strings such as string concatenation, string cutting, etc. creates a new string on memory, or return another string.
The concat(string) method is used to concatenate a string into the current string. But in fact the current string does not change, and a new string (the result of concatenating) is created. The  following figure illustrates program behavior: 
let a = "Hello";
let b = "World";

let c = a.concat(b);

console.log(c); // HelloWorld
console.log(a); // Hello

console.log( a === c); // false

3- Các Properties của String

  • constructor
  • length
  • prototype


This property returns the number of characters in a string.
let s1 = "Tom";

console.log(s1); // Tom
console.log("Length "+ s1.length); // 3

let s2 = "Tom & Jerry";

console.log(s2); // Tom & Jerry
console.log("Length "+ s2.length); // 11

4- String Template


View more Tutorials: