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"


n = 4

then the output should be −

const output = " is a string"


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));


The output in the console −

s is a string
Published on 15-Oct-2020 12:18:35