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?

2

2 Answers 2

4

Try using the slice function:

var str = "Hello world!"; 
var res = str.slice(2, 5); 
console.log(res) // llo
Sign up to request clarification or add additional context in comments.

2 Comments

probably want to change the slice from 2 instead of 0.
@Gabip Apologies for editing the doc link but w3schools is not a good reference resource.
1

You can use both slice or substring:

const str = "Hello world!";

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

2 Comments

What is the difference between them two?
Minimal differences... with substring you can switch the start/end order and still works (If "start" is greater than "end", it will swap the two arguments). With slice you can use negative number to start from the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.