# How To Remove File Extension From A String Using JavaScript?

Remove file extension from a string using JavaScript is one of the important task when working with data. We will give you some methods in this article: Use split() and slice() method or use []. Let’s see the specific example below.

## File extension

The term “extension” refers to the suffix at the end of a file name describing its sort of file. For instance, the file extension is .TXT in the file name “text.txt”. The file is a text document. Other examples are the.docx file extension, which is used for Microsoft Word documents, and the PSD file extension, which is the default for Photoshop documents.

Examining the file’s extension allows an operating system to determine which application to read, print, or edit a particular file. Because each operating system in the setup has a default mapping between a file extension and a specific program, it can do this function.

## Remove file extension from a string using JavaScript

### Use split() and slice() method

The split method of a string object is used to split a string into an array of substrings and return the new array.

Syntax:

string.split(separator, limit)

Parameters:

• Separator: (Optional) Specifies the character, or regular expression, to use to split the string. If omitted, the entire string is returned (an array with only one item).
• limit: (Optional) An integer specifying the number of splits, items after the division limit will not be included in the array.

The String slice() method in JavaScript extracts an area of ​​a string and returns a new string.

Syntax:

string.slice( begin slice [, endSlice] );

Parameters:

• startSlice: The index at which the extraction should begin.
• endSlice: The index where the program should stop the extraction. Slice extracts to the end of the string if it is omitted.

After knowing about the method to use this way, let’s see the example below to understand more about how to do it.

Code:

var file = 'number.txt';
var filename = file.split('.').slice(0, -1).join('.');
console.log(filename);

Output:

number

First, we separate the filename from the file extension using the split method where the “.” then we use the slice complex method to remove the extension. The program will return an array with a string named file name, so we use the join method to get that value.

### Refer to the index position with []

Like the above method, we use the split method to split into two strings, filename and file extension. But in this way, we use [] to point to the string with index 0, and we can get the string filename and print it to the console.

Code:

var file = 'number.txt';
var filename = file.split('.')[0];
console.log(filename);

Output:

number

So we obtained the filename string of the file and printed it to the screen. Hope it helps you.

## Summary

To summarize, there are many ways to remove file extensions from a string using JavaScript. You can absolutely choose any method that works for you. Thanks for reading!
Maybe you are interested: