Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

JavaScript - remove first 3 characters from string

0 contributions
9 points

In JavaScript it is possible to remove first 3 characters from string in following ways.

1. Using String slice() method

// ONLINE-RUNNER:browser;

var text = "12345";
var substring = text.slice(3);

console.log(substring); // 45

In second example we can see that we can also use string slice on empty or shorter string then 3 characters (or in case if we want to remove n characters from our string).

// ONLINE-RUNNER:browser;

// true means string is empty
console.log( "".slice(3) === "" ); // empty
console.log( "1".slice(3) === "" ); // empty
console.log( "12".slice(3) === "" ); // empty
console.log( "123".slice(3) === "" ); // empty
console.log( "1234".slice(3) ); // 4
console.log( "12345".slice(3) ); // 45
console.log( "123456".slice(3) ); // 456
console.log( "1234567".slice(3) ); // 4567

We use "".slice(3) === "" to see that the string after slice operation is empty.

Empty result means that string was shorter then 3 characters or the length was exactly 3 characters and our string is empty (the length of the string is 0).

Because string slice returns string then we can also call string length method to check the size of returned string, like on below example:

// ONLINE-RUNNER:browser;

console.log( "".slice(3) === "" ); // empty
console.log( "".slice(3).length ); // 0

2. Using String substring() method

// ONLINE-RUNNER:browser;

var text = "12345";
var substring = text.substring(3);

console.log(substring); // 45

3. Using String replace() method

// ONLINE-RUNNER:browser;

var text = "12345";
var substring = text.replace(/.{3}/, '');

console.log(substring); // 45

Merged questions

  1. JavaScript - how to chop off first 3 chars from string?
  2. How do I trim off first 3 characters in JavaScript?
  3. JavaScript - delete first 3 chars of string
  4. Slice off first 3 characters in js
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more