ECMAScript 3 (ES3), released in 1999, marked a significant update to the ECMAScript standard, introducing a range of features that greatly enhanced the language's capabilities. This version of the ECMAScript specification laid the groundwork for many of the JavaScript features and behaviors web developers rely on today. Here are some of the key features introduced in ES3, along with examples:
1. Regular Expressions
ES3 introduced support for regular expressions, providing a powerful tool for string searching and manipulation.
var regex = /hello/;
console.log(regex.test("hello world")); // Outputs: true
2. Try/Catch for Error Handling
Error handling was improved with the introduction of try/catch blocks, allowing developers to handle exceptions more gracefully.
try {
throw new Error("Something went wrong");
} catch (e) {
console.log(e.message); // Outputs: Something went wrong
}
3. More String Methods
New string manipulation methods were added, such as slice, split, and replace, enhancing the ability to work with strings.
var text = "hello world";
console.log(text.slice(0, 5)); // Outputs: hello
console.log(text.split(" ")); // Outputs: ["hello", "world"]
console.log(text.replace("hello", "goodbye")); // Outputs: goodbye world
4. New Array Methods
ES3 introduced new methods to manipulate arrays, such as concat, join, push, pop, shift, and unshift.
var fruits = ["apple", "banana"];
fruits.push("orange");
console.log(fruits); // Outputs: ["apple", "banana", "orange"]
5. Tightened Syntax Rules
The syntax rules became stricter in ES3 to reduce errors and ambiguities in code. For example, ES3 disallowed the use of a reserved word as an identifier in strict mode.
// In ES3, this would be considered an error in strict mode
var float = 3.14;
6. Format Control Characters
ES3 introduced support for format control characters, allowing for the inclusion of Unicode format-control characters in source text.
7. More Control Over Primitive Values
ES3 clarified the behavior of primitive values like null and undefined, and how they interact with operators and functions.
8. Enhanced Functionality for Functions
Functions gained the ability to be assigned to variables, passed as arguments, and returned from other functions, reinforcing the concept of first-class functions.
function createGreeting(name) {
return function(greeting) {
console.log(greeting + ", " + name);
};
}
var greetJohn = createGreeting("John");
greetJohn("Hello"); // Outputs: Hello, John
ECMAScript 3 significantly advanced the JavaScript language, introducing features that addressed common programming needs and pain points. By enhancing string and array manipulation capabilities, improving error handling, and tightening syntax rules, ES3 made JavaScript more powerful, flexible, and easier to work with. These improvements laid the groundwork for future enhancements and solidified JavaScript's role as a foundational web technology.