How to use JavaScript String slice()

String slice

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

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

String slice()

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

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