Trim white spaces from a String in Javascript

Trim white spaces

The trim() method removes all leading and trailing white space:


var stringValue = " hello world "; 
var trimmedStringValue = stringValue.trim(); 
console.log(stringValue); //" hello world " 
console.log(trimmedStringValue); //"hello world" 
/*from  w  ww.ja v  a  2s . c om*/

The original string remains intact.

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window