Difference between JavaScript's trimLeft() vs trimStart()
The trimStart()
method removes whitespace
from the beginning of a string. trimLeft()
is an alias of this method.
So both work same both return the same value. The example of proof is attached.
var greeting = " Hello world! ";
console.log(greeting);
// expected output: " Hello world! ";
console.log("output:" + greeting.trimStart() + ";", "output:" + greeting.trimLeft() + ";");
// expected output: "Hello world! ";
console.log(greeting.trimStart() === greeting.trimLeft());
// expected output: true;
MDN states that trimLeft
is an alias of trimStart
.
The property
trimStart
is preferred. ThetrimLeft
property is provided principally for compatibility with old code. It is recommended that thetrimStart
property be used in new ECMAScript code.
However, trimLeft()
or trimStart()
do not affect the value of the string itself.
var greeting = " Hello world! ";
console.log(greeting);
// expected output: " Hello world! ";
console.log("trimStart:" + greeting.trimStart() + ";"); console.log("trimLeft:" + greeting.trimLeft() + ";");
// expected output: "Hello world! ";
var nTrimVal = greeting.trimStart();
var nTrimNormal = " Hello world! ".trim();
console.log("Original String: ", greeting);
console.log("String value and new Trim value is same: ", greeting == nTrimVal);