Lowercasing a String
Problem
You want to lowercase a string.
Solution
Use JavaScript’s String toLowerCase() method:
Discussion
toLowerCase()
is a standard JavaScript method. Don’t forget the parentheses.
Syntax Sugar
You can add some Ruby-like syntax sugar with the following shortcut:
The snippet above demonstrates a few features of CoffeeScript:
- The double-colon
::
is shorthand for saying.prototype.
- The “at” sign
@
is shorthand for sayingthis.
The code above compiles in to the following JavaScript:
Note: Although it’s quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: Maintainable JavaScript: Don’t modify objects you don’t own; Extending built-in native objects. Evil or not?).