Replacing spaces with underscores in JavaScript Last Updated : 20 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a sentence and the task is to replace the spaces(" ") from the sentence with underscores("_") in JavaScript. There are some JavaScript methods are used to replace spaces with underscores which are listed below: JavaScript replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: It is required parameter. It specifies the value, or regular expression, that is going to replace by the new value.newvalue: It is required parameter. It specifies the value to replace the search value with. JavaScript split() method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split(separator, limit) Parameters: separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item).limit: It is optional parameter. It holds the integer that specifies the number of splits, items beyond the split limit will be excluded from the array. Example 1: This example replaces all spaces(' ') with underscores("_") by using replace() method. JavaScript let str = "A Computer Science portal for Geeks."; console.log(str); console.log(str.replace(/ /g, "_")); OutputA Computer Science portal for Geeks. A_Computer_Science_portal_for_Geeks. Example 2: This example replaces all spaces(' ') with underscores("_") by using split() method. It first splits the string with spaces(" ") and then join it with underscore("_"). JavaScript let str = "A Computer Science portal for Geeks."; console.log(str); console.log(str.split(' ').join('_')); OutputA Computer Science portal for Geeks. A_Computer_Science_portal_for_Geeks. Comment More infoAdvertise with us Next Article Replacing spaces with underscores in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Replace special characters in a string with underscore (_) in JavaScript We will explore several methods to replace special characters in a string with an underscore (_) in JavaScript. Special characters are non-alphanumeric symbols, and replacing them is a common requirement in various use cases such as sanitizing input or formatting file names.Table of ContentJavaScrip 2 min read What does +_ operator mean in JavaScript ? Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used t 2 min read Underscore.js _.without() Function Underscore.js _.without() function is used to return a copy of the array which contains all the array except values. Syntax:_.without( array, *values );Parameters:array: This parameter is used to hold the list of array elements.values: This parameter is used to hold the value that needs to be remove 3 min read Underscore.js _.isString() Function The _.isString() function is used to check whether the given object element is string or not. Syntax: _.isString( object ) Parameters: This function accepts single parameter as mentioned above and described below: object: It contains the value of object that need to be check whether it is an string 1 min read Underscore.js _.pairs() Function Underscore.js _.pairs() function is used to convert an object into an array of arrays that contain the [key, value] pairs of the object as elements. Syntax:_.pairs( object );Parameters:object: It contains the object element that holds the elements of key and value pair.Return Value:It returns the ar 1 min read Like