JavaScript

June 6, 2023

How to use JavaScript String slice()

Program to use JavaScript String slice()


let a = 'Hello World JavaScript';
let b = a.slice(0, 11);

console.log(b);

Output


Hello World

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of string a = 'Hello World JavaScript' and b = a.slice(0, 11)
  3. String slice() method String can be sliced by giving statement
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String slice()


let c = 'Hello World JavaScript';
let d = c.slice(11, 22);

console.log(c.length);
console.log(d);

Output


22
JavaScript

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of string c = 'Hello World JavaScript' and d = c.slice(11, 22)
  3. String slice() method String can be sliced by giving statement
  4. The value of d variable is displayed with the help of console.log inbuilt function

by : Suhel Akhtar

Quick Summary:

JavaScript String slice()