A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-es5-js-2009/ below:

JavaScript ES5 (JS 2009) - GeeksforGeeks

JavaScript ES5 (JS 2009)

Last Updated : 23 Jul, 2025

JavaScript 2009 (ES5) refers to the fifth edition of the ECMAScript language specification, standardized in 2009. It introduced several features, like strict mode, new methods, JSON support, and improved syntax for better programming practices and compatibility across browsers.

ECMAScript 5 (ES5) introduced several significant features to JavaScript, enhancing the language's capabilities.

Enforces stricter parsing and error handling in JavaScript code.

Removes whitespace from the beginning and end.

Use backslashes at the end of each line to continue.

Check if an object is an array.

Iterates over array elements, applying a provided function.

Transforms each array element using a provided function.

Creates a new array with elements passing a test.

Combines an array of elements to produce a single result.

Combines array elements from right to left.

Checks if all elements satisfy a given condition.

Checks if any element satisfies a given condition.

Returns the index of a specified element.

Returns the last index of a specified element.

Converts JSON string to JavaScript object.

Converts JavaScript object to JSON formatted string.

Returns the current timestamp in milliseconds since 1970.

Converts date to ISO 8601 format string.

Converts date to JSON-formatted string (ISO 8601).

Control object property access and modification behavior.

Defines or modifies a property's attributes on an object.

Defines or modifies multiple properties' attributes on an object.

Gets attributes of a property from an object.

Returns an array of enumerable property names in object.

Returns an array of all property names in object.

Prevents adding new properties to an object.

Checks if new properties can be added.

Prevents new properties and makes existing non-configurable.

Checks if object properties are non-configurable and sealed.

Prevents adding, modifying, and makes properties non-writable.

Checks if object properties are non-writable, non-configurable, and frozen.

Here is the explanation of above-mentioned topic

Method 1: use strict

The use strict enforces strict mode in JavaScript, catching errors and promoting better coding practices. It's used at the beginning of scripts or functions.

Syntax:

// Whole-script strict mode syntax
'use strict';
let v = "strict mode script!";

Example: In this example we are using the above-explained method.

JavaScript
"use strict";
function strictFunction(a, b) {
    // Throws an error in strict mode 
    //due to the undeclared variable "result"
    result = a + b;
    return result;
}

let sum = strictFunction(5, 10);
console.log("Sum of the given numbers :", sum);

Output:

ReferenceError: result is not defined
Method 2: String.trim() Method

JavaScript String trim() method is used to remove the white spaces from both ends of the given string.

Syntax:

str.trim()

Example: In this example we are using the above-explained method.

JavaScript
function func() {
    let str = "GeeksforGeeks     ";
    let st = str.trim();
    console.log(st);
}

func();
Method 3: Multiline Strings

In ES5, multiline strings can be created using backslashes at the end of each line to continue, combined with + for concatenation, to form a single string across lines.

Syntax:

line one\n\
line two\n\

Example: In this example we are using backslashes and + to write Multiline strings.

JavaScript
let str1 = "GeeksforGeeks \
A computer science \
Portal.";

console.log(str1);

let str2 = "Geeksfor" +
    "Geeks"

console.log(str2);

Output
GeeksforGeeks A computer science Portal.
GeeksforGeeks
Method 4: Array Methods (Array.isArray(), Array.map(), Array.filter(), and Array.reduce() Methods)

Syntax:

Array.isArray(obj) 
map((element) => { /* … */ })
array.filter(callback(element, index, arr), thisValue)
array.reduce( function(total, currentValue, currentIndex, arr), initialValue )

Example: In this example we are using the above-explained methods.

JavaScript
// Step 1: Array.isArray()
let numbers = [1, 2, 3, 4, 5];
let isNumbersArray = Array.isArray(numbers);
console.log("Is 'numbers' an array?", isNumbersArray);

// Using Array.map()
let doubledNumbers = numbers.map(function (number) {
    return number * 2;
});
console.log("doubling each elemen of our array :", doubledNumbers);

// Using Array.filter()
let evenNumbers = numbers.filter(function (number) {
    return number % 2 === 0;
});
console.log("Even numbers from the array:", evenNumbers);

// Using Array.reduce()
let sum = numbers.reduce(function (accumulator, currentValue) {
    return accumulator + currentValue;
}, 0);
console.log("Sum of all elements in the array:", sum);

Output
Is 'numbers' an array? true
doubling each elemen of our array : [ 2, 4, 6, 8, 10 ]
Even numbers from the array: [ 2, 4 ]
Sum of all elements in the array: 15
Method 5: JSON.parse() Method

The JSON.parse() method is a JavaScript method that converts a JSON string into a JavaScript object.

Syntax:

JSON.parse( string, function )

Example: Here is the basic example of JSON.parse() method.

JavaScript
let str1 = '{"name": "Aman", "age": 21, "city": "Noida"}';

let result = JSON.parse(str1);

console.log(result.name);
console.log(result.age);
console.log(result.city);
Method 6: JSON.stringify() Method

JSON.stringify() is a JavaScript method that converts a JavaScript object into a JSON string for data serialization and storage.

Syntax:

JSON.stringify(value, replacer, space);

Example: In this example we are using JSON.stringify().

JavaScript
const value = {
    Company: "GeeksforGeeks",
    Estd: 2009,
    location: "Noida"
};
const result = JSON.stringify(value);
console.log("value of result = " + result);

Output
value of result = {"Company":"GeeksforGeeks","Estd":2009,"location":"Noida"}
Date Methods (Date.now(),Date toISOString() and Date toJSON() Methods)

Syntax:

let A = Date.now();  // Date.now()
dateObj.toISOString() // Date toISOString()
dateObj.toJSON() // Date toJSON()

Example: In this example we are using the abobe-explained methods.

JavaScript
// Using new Date
let currentDate = new Date();

// Using Date.now() 
let timestamp = Date.now();
console.log("Timestamp:", timestamp);

// Using Date.toISOString() 
let isoString = currentDate.toISOString();
console.log("ISO String:", isoString);

// Using Date.toJSON() 
let jsonDate = currentDate.toJSON();
console.log("JSON Date:", jsonDate);

Output
Timestamp: 1698213643835
ISO String: 2023-10-25T06:00:43.835Z
JSON Date: 2023-10-25T06:00:43.835Z
Method 7: Property Getters and Setters

In ECMAScript 5 (ES5), property getters and setters enable defining custom behavior when reading (get) or modifying (set) object properties, enhancing encapsulation and control over property access.

Example: In this example we are using the above-explained approach.

JavaScript
const person = {
    firstName: "John",
    lastName: "Doe",
    get fullName() {
        return `${this.firstName} ${this.lastName}`;
    },
    set fullName(name) {
        const parts = name.split(" ");
        this.firstName = parts[0];
        this.lastName = parts[1];
    },
};

console.log(person.fullName);

person.fullName = "Jane Smith";
console.log(person.firstName);
console.log(person.lastName); 

Output
John Doe
Jane
Smith
Method 8: New Object Property Methods

ECMAScript 5 introduced new object methods

It enhance the object property control, definition, and manipulation.

Syntax:

Object.defineProperty(obj, prop, descriptor)   // Object.defineProperty()
Object.defineProperties(obj, props) // Object.defineProperties()
Object.getOwnPropertyDescriptor( obj, prop ) // Object.getOwnPropertyDescriptor()
Object.keys(obj); // Object keys() Method

Example: In this example we are some mentioned method.

JavaScript
// Define an empty object
let person = {};

// Define properties using Object.defineProperty()
Object.defineProperty(person, 'name', {
    value: 'Ankit',
    writable: false,
    enumerable: true,
    configurable: false
});

// Define multiple properties using 
// Object.defineProperties() method
Object.defineProperties(person, {
    age: {
        value: 21,
        writable: true,
        enumerable: true,
        configurable: true
    },
    gender: {
        value: 'Male',
        writable: true,
        enumerable: true,
        configurable: true
    }
});

// Get enumerable property names using 
// Object.keys() method
let propertyNames = Object.keys(person);
console.log('Property Names:', propertyNames);

// Get all property names using 
// Object.getOwnPropertyNames() method
let allPropertyNames = Object.getOwnPropertyNames(person);
console.log('All Property Names:', allPropertyNames);

// Prevent adding new properties using 
// Object.preventExtensions() method
Object.preventExtensions(person);
console.log('Is Extensible:', Object.isExtensible(person));

// Seal the object using Object.seal()
Object.seal(person);
console.log('Is Sealed:', Object.isSealed(person));

// Attempt to add a new property after sealing 
// (fails due to Object.seal())
person.city = 'Delhi';

// Freeze the object using Object.freeze()
Object.freeze(person);
console.log('Is Frozen:', Object.isFrozen(person));

// Try to modify properties after freezing (all fail)
person.name = 'Nikita'; // Fails due to 'writable: false'
person.age = 35; // Fails due to Object.freeze()
person.gender = 'Female'; // Fails due to Object.freeze()

console.log(person);

Output
Property Names: [ 'name', 'age', 'gender' ]
All Property Names: [ 'name', 'age', 'gender' ]
Is Extensible: false
Is Sealed: true
Is Frozen: true
{ name: 'Ankit', age: 21, gender: 'Male' }


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4