1

In Python, you can do something like this:

string = "Hello World"
print(string[2:5])
# llo

What is the equivalent of that in JavaScript?

Gabio
  • 9,126
  • 3
  • 12
  • 32
leo848
  • 637
  • 2
  • 7
  • 24

2 Answers2

4

Try using the slice function:

var str = "Hello world!"; 
var res = str.slice(2, 5); 
console.log(res) // llo
Nick
  • 138,499
  • 22
  • 57
  • 95
Gabio
  • 9,126
  • 3
  • 12
  • 32
1

You can use both slice or substring:

const str = "Hello world!";

console.log(str.substring(2, 5));
console.log(str.slice(2, 5));