ECMAScript 2009, or ES5, was the first major update to JavaScript.
This chapter covers the key features introduced in ES5.
ES5 (JavaScript 2009) has been fully supported in all modern browsers since July 2013.
The “use strict” directive enforces “strict mode” in JavaScript.
In strict mode, for instance, using undeclared variables is not allowed.
You can apply strict mode to all your programs to help write cleaner code, such as preventing the use of undeclared variables.
The “use strict” directive is simply a string expression, and older browsers won’t throw an error if they don’t recognize it. |
The charAt() method returns the character at a specified index (position) within a string.
var str = “HELLO WORLD”; str.charAt(0); // returns H |
ES5 allows accessing properties on strings.
var str = “HELLO WORLD”; str[0]; // returns H |
ES5 allows string literals to span multiple lines if they are escaped with a backslash.
“Hello \ Dolly!”; |
A safer way to break up a string literal is by using string concatenation.
“Hello “ + “Dolly!”; |
ES5 allows reserved words to be used as property names.
var obj = {name: “John”, new: “yes”} |
The trim() method removes whitespace from the beginning and end of a string.
var str = ” Hello World! “; alert(str.trim()); |