Languages
[Edit]
EN

JavaScript - remove first n characters from string - js util

17 points
Created by:
Marcin
15740

In JavaScript it is possible to remove first n characters from string in following way.

1. Using String slice() method

// ONLINE-RUNNER:browser;

// remove first n characters from string
function removeFirstChars(text, n) {
    return text.slice(n);
}

console.log( removeFirstChars('12345', 0) ); // 12345
console.log( removeFirstChars('12345', 1) ); // 2345
console.log( removeFirstChars('12345', 2) ); // 345
console.log( removeFirstChars('12345', 3) ); // 34
console.log( removeFirstChars('12345', 4) ); // 5

// below results are empty, that's why we compare them to ''
console.log( removeFirstChars('12345', 5) === '' ); // true

Note:
We can achieve the same results using:

  • String substring() method
  • String replace() method
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join