
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- Remove all characters of first string from second JavaScript
- JavaScript Remove non-duplicate characters from string
- Removing first k characters from string in JavaScript
- Remove characters from a string contained in another string with JavaScript?
- How to extract the first n characters from a string using Java?
- Removing n characters from a string in alphabetical order in JavaScript
- C# Program to remove duplicate characters from String
- Write a Regular Expression to remove all special characters from a JavaScript String?
- PHP program to remove non-alphanumeric characters from string
- How to Remove Characters from a String in Arduino?
- How to remove certain characters from a string in C++?
- How to remove specific characters from a string in Python?
- How to remove characters except digits from string in Python?
- Remove all whitespaces from string - JavaScript
- How to get first N characters from a MySQL column?

Advertisements