Removing first k characters from string in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string.

For example: If the original string is −

const str = "this is a string"

and,

n = 4

then the output should be −

const output = " is a string"

Example

The code for this will be −

const str = 'this is a string';
const removeN = (str, num) => {
   const { length } = str;
   if(num > length){
      return str;
   };
   const newStr = str.substr(num, length - num);
   return newStr;
};
console.log(removeN(str, 3));

Output

The output in the console −

s is a string
raja
Published on 15-Oct-2020 08:48:35
Advertisements