Remove spaces

2 snippets across 2 stacks - JavaScript, SQL

JSJavaScript

Trimming Whitespace

JS · Strings
Syntax
str.trim()
str.trimStart()
str.trimEnd()
Example
const input = "  [email protected]  ";
console.log(input.trim());       // "[email protected]"
console.log(input.trimStart());  // "[email protected]  "
console.log(input.trimEnd());    // "  [email protected]"

Note Essential for cleaning user input. Only removes whitespace characters (spaces, tabs, newlines), not other invisible characters.

SQLSQL

TRIM / LTRIM / RTRIM

SQL · String Functions
Syntax
TRIM([LEADING|TRAILING|BOTH] [chars] FROM string)
LTRIM(string)
RTRIM(string)
Example
SELECT
  TRIM('  Alice  ') AS trimmed,
  TRIM(LEADING '0' FROM '000425') AS no_leading_zeros,
  LTRIM('  hello') AS left_trimmed,
  RTRIM('hello  ') AS right_trimmed;
Output
-- trimmed | no_leading_zeros | left_trimmed | right_trimmed
-- Alice   | 425              | hello        | hello

Note TRIM with no arguments removes whitespace from both sides. You can specify characters to trim. LTRIM and RTRIM are shorthand for leading/trailing trim. Clean user input with TRIM before storing.

Frequently asked questions

How do you remove spaces?
This task is covered in 2 stacks on this page: JavaScript, SQL. The "Trimming Whitespace" snippet in JavaScript uses `str.trim()`.
Which code does the JavaScript example use?
The "Trimming Whitespace" snippet uses `str.trim()`, from the Strings section of the JavaScript cheat sheet.
Which stacks cover "remove spaces" on this page?
JavaScript, SQL. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Trimming Whitespace": Essential for cleaning user input. Only removes whitespace characters (spaces, tabs, newlines), not other invisible characters.