ECMAScript 2009, also called ES5, marked a significant update for JavaScript, being its initial major revision.
This section explains the key aspects of ES5.
ES5 Features
The "use strict" Directive
The code ""use strict"
tells JavaScript to run the code in a stricter mode.
Strict mode prevents the use of variables that haven't been declared, for instance.
You can apply strict mode to all your programs. This mode helps you write neater code by stopping you from using variables that haven't been declared.
The phrase ""use strict"
is a simple text. Older web browsers won't show an error if they don't recognize it.
Property Access on Strings
The charAt()
function gives you the character at a particular position in a string.
In ES5, you can access properties on strings.
Accessing information in a string could be somewhat unpredictable.
Learn more by visiting JS String Methods.
Strings Over Multiple Lines
In ES5, you can use a backslash to allow string literals to span multiple lines.
The \ method may not work everywhere.
Older browsers could handle spaces around the backslash in a different way.
Some older browsers may not permit spaces after the \ character.
A more secure method to split a string literal is by employing string addition.
Reserved Words as Property Names
ES5 permits using reserved words as names for properties.
String trim()
The trim()
function gets rid of extra spaces at the beginning and end of a text.
Array.isArray()
The isArray()
function verifies if an object is an array.
Array forEach()
The forEach()
function is used to execute a specific function for every element in an array.
Array map()
This example doubles each value in an array by multiplying it by 2.
Array filter()
This example forms a fresh array by selecting elements that have a value greater than 18.
Array reduce()
This example calculates the total of all numbers in a list:
Array reduceRight()
This illustration calculates the total of numbers within an array.
Array every()
This sample verifies whether all the values are greater than 18.
Array some()
This sample verifies whether certain values are greater than 18.
Array indexOf()
Look for a specific value in an array and find its position.
Array lastIndexOf()
The lastIndexOf()
function works like indexOf()
, but it looks for an element starting from the end of the array.
JSON.parse()
JSON is often used to get data from a web server.
Picture getting this bunch of words from a web server:
'{"name":"John", "age":30, "city":"New York"}'
The JavaScript code JSON.parse()
changes text into a JavaScript object.
JSON.stringify()
JSON is often used to transmit information to a web server.
When you send information to a web server, it needs to be in the form of a string.
Picture having this thing in JavaScript:
var obj = {name:"John", age:30, city:"New York"};
Employ the JavaScript function JSON.stringify()
to transform it into a string.
var myJSON = JSON.stringify(obj);
The outcome will be a text formatted using JSON style.
myJSON is now in string format and prepared to be sent to a server:
Date.now()
The code Date.now()
gives you the count of milliseconds passed since the zero date, which is January 1, 1970, 00:00:00 UTC.
The code Date.now()
gives the same result as calling getTime() on a Date
object.
Date toISOString()
The toISOString()
function changes a Date object into a string, following the ISO standard format.
Date toJSON()
The toJSON()
function changes a Date object into a string, arranged in the JSON date format.
JSON dates follow the ISO-8601 standard format: YYYY-MM-DDTHH:mm:ss.sssZ.
Property Getters and Setters
ES5 allows you to create object methods using a syntax that resembles defining or updating a property.
This example makes a getter for a property named fullName:
This illustration demonstrates how to establish a setter and a getter for the language property.
This illustration employs a setter to ensure that language updates are limited to uppercase letters.
Object.defineProperty()
The Object.defineProperty()
is a recently introduced method for Objects in ES5.
You can set up a characteristic of an object, modify the value of a characteristic, and/or add additional information about a characteristic.
The following example shows the same code, but it conceals the language property when listing.
This example makes a method to safely change and retrieve uppercase language updates.
E5 Object Methods
ES5 introduced many new methods for objects in JavaScript.
Managing Objects
Object.create(parent, donor)
Object.defineProperty(object, property, descriptor)
Object.defineProperties(object, descriptors)
Object.getOwnPropertyDescriptor(object, property)
Object.getOwnPropertyNames(object)
Object.getPrototypeOf(object)
Object.keys(object)
Protecting Objects
Object.preventExtensions(object)
Object.isExtensible(object)
Object.seal(object)
Object.isSealed(object)
Object.freeze(object)
Object.isFrozen(object)
Function Bind()
The bind()
method allows an object to use a method from another object.
This example makes two objects called "person" and "member."
The member object uses the fullname method from the person object.
Trailing Commas
ES5 permits the use of commas at the end of object and array definitions.
Object Example
person = {
firstName: "John",
lastName: "
Doe",
age: 46,
}
Array Example
points = [
1,
5,
10,
25,
40,
100,
];
WARNING !!!
JSON doesn't permit using commas at the end.
JSON Objects:
var person = '{"firstName":"John", "lastName":"Doe",
"age":46}'
JSON.parse(person)
var person = '{"firstName":"John",
"lastName":"Doe", "age":46,}'
JSON.parse(person)
JSON Arrays:
points = [40, 100, 1, 5, 25, 10]
points =
[40, 100, 1, 5, 25, 10,]
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>The charAt() function gives you the letter found at a specific place in a word.</p>
<p id="demo"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo").innerHTML = str.charAt(0);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>ECMAScript 5 allows property acces on strings:</p>
<p id="demo"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo").innerHTML = str[0];
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p> You can split a line of code inside a text by using a backslash. </p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello \
Dolly!";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<p>The safest method to split a line of code within a string is by adding strings together.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello " + "Dolly!";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>ECMAScript 5</h1>
<p>ECMAScript 5 allows reserved words as property names.</p>
<p id="demo"></p>
<script>
var obj = {
name: "John",
new: "yes"
};
document.getElementById("demo").innerHTML = obj.new;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The trim() Method</h2>
<p id="demo"></p>
<script>
let text1 = " Hello World! ";
let text2 = text1.trim();
document.getElementById("demo").innerHTML = "Length text1 = " + text1.length + " <br> Length text2 = " + text2.length;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The isArray() Method</h2>
<p>Click the button to check if "fruits" is an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.forEach()</h2>
<p>Calls a function once for each array element.</p>
<p id="demo"></p>
<script>
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;
function myFunction(value) {
txt = txt + value + " <br> ";
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.map()</h2>
<p>Makes a fresh list by doing something to each item in the existing list.</p>
<p id="demo"></p>
<script>
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
document.getElementById("demo").innerHTML = numbers2;
function myFunction(value, index, array) {
return value * 2;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.filter()</h2>
<p>Creates a fresh array containing only the elements from the original array that meet a specific condition.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
document.getElementById("demo").innerHTML = over18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.reduce()</h2>
<p>This example finds the sum of all numbers in an array:</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var sum = numbers.reduce(myFunction);
document.getElementById("demo").innerHTML = "The sum is " + sum;
function myFunction(total, value, index, array) {
return total + value;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.reduceRight()</h2>
<p>This sample calculates the total of all numbers in a list.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var sum = numbers.reduceRight(myFunction);
document.getElementById("demo").innerHTML = "The sum is " + sum;
function myFunction(total, value) {
return total + value;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.every()</h2>
<p>The every() method checks if all array values pass a test.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.every(myFunction);
document.getElementById("demo").innerHTML = "All over 18 is " + allOver18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.some()</h2>
<p>The some() function checks if any of the values in an array meet a certain condition.</p>
<p id="demo"></p>
<script>
var numbers = [45, 4, 9, 16, 25];
var allOver18 = numbers.some(myFunction);
document.getElementById("demo").innerHTML = "Some over 18 is " + allOver18;
function myFunction(value) {
return value > 18;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The indexOf() Method</h2>
<p id="demo"></p>
<script>
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;
document.getElementById("demo").innerHTML = "Apple is found in position " + position;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The lastIndexOf() Method</h2>
<p id="demo"></p>
<script>
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;
document.getElementById("demo").innerHTML = "Apple is found in position " + position;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>Creating an Object from a JSON String</h2>
<p id="demo"></p>
<script>
const txt = '{"name":"John", "age":30, "city":"New York"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>Create a JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<script>
const obj = {
name: "John",
age: 30,
city: "New York"
};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date.now()</h2>
<p>Date.now() is new in ECMAScript 5 (2009):</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
document.getElementById("demo1").innerHTML = Date.now();
var d = new Date();
document.getElementById("demo2").innerHTML = d.getTime();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toISOString() Method</h2>
<p>Convert a date to a string using the ISO standard:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date.toJSON()</h2>
<p id="demo"></p>
<script>
d = new Date();
document.getElementById("demo").innerHTML = d.toJSON();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters help you to access and modify properties using methods.</p>
<p>This example shows how to make a way to get a property named fullName.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName: "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters help you to access and change properties using special functions.</p>
<p>This example makes a way to change and get information about the language used.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName: "Doe",
language: "NO",
get lang() {
return this.language;
},
set lang(value) {
this.language = value;
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Getters and Setters</h2>
<p>Getters and setters let you fetch and update properties using functions.</p>
<p>This sample code has a changed function that ensures only capital letters are used when updating the language.</p>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName: "Doe",
language: "",
set lang(value) {
this.language = value.toUpperCase();
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
</head>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName: "Doe",
language: "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable: true,
enumerable: true,
configurable: true
});
// Enumerate Properties
txt = "";
for (var x in person) {
txt += person[x] + " <br> ";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName: "Doe",
language: "NO",
};
// Change a Property:
Object.defineProperty(person, "language", {
value: "EN",
writable: true,
enumerable: false,
configurable: true
});
// Enumerate Properties
txt = "";
for (var x in person) {
txt += person[x] + " <br> ";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript defineProperty()</h2>
<p id="demo"></p>
<script>
// Create an Object:
var person = {
firstName: "John",
lastName: "Doe",
language: "NO"
};
// Change a Property:
Object.defineProperty(person, "language", {
get: function() {
return language
},
set: function(value) {
language = value.toUpperCase()
}
});
// Change language
person.language = "en";
// Display language
document.getElementById("demo").innerHTML = person.language;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Function bind()</h1>
<p>This example creates 2 objects (person and member).</p>
<p>The member object uses the fullname function from person.</p>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName: "Hege",
lastName: "Nilsen",
}
let fullName = person.fullName.bind(member);
document.getElementById("demo").innerHTML = fullName();
</script>
</body>
</html>