
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove First K Characters from String in JavaScript
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
Advertisements