![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Remove first n characters from string
We are required to write a JavaScript function that takes in a string and a number n and returns another string with first n characters removed from the string.
For example −
If the original string is −
const str = "this is a string" and n = 5,
then the output should be −
const output = "is a string"
Let’s write the code for this function −
Following is the code −
const mobileNumber = '+915389534759385'; const secondNumber = '+198345985734'; const removeN = (str, num) => { const { length } = str; if(num > length){ return str; }; const newStr = str.substr(num, length - num); return newStr; }; console.log(removeN(mobileNumber, 3)); console.log(removeN(secondNumber, 2));
Output
Following is the output in the console −
5389534759385 98345985734
Advertisements