• Diffrent types of Methods
    • length(): Determines and returns the total number of characters in a string.
    • indexOf(): Finds and returns the position of a specified character or substring within a string.
    • charAt(): Retrieves the character located at a specific index in the string.
    • substring(): Extracts and returns a part of the string based on specified start and end indices.
    • toUpperCase(): Converts all characters in the string to uppercase.
    • toLowerCase(): Converts all characters in the string to lowercase.
    • trim(): Removes any leading and trailing spaces from the string.

also you an use methos to create arrays also for methods which is super efficent for conditonals or loop in your game like in quest where you have to collect or talk with diffrent npcs

let numbers = [17, 22, 35, 41, 58];

// Display the total number of elements
console.log("Total elements:", numbers.length);

// Access and display the fourth element
console.log("Fourth element:", numbers.at(3));

Instating Object:

Instantiating objects involves creating a JSON object that can be easily utilized in object-oriented programming (OOP). To instantiate an object, you use a class function, which is similar to a JSON object but offers more versatility. Unlike JSON objects that primarily store data, classes can encapsulate data, functions, and additional logic, making them a powerful tool for structuring and managing complex programs.

class Animal {
    constructor(species, name, age) {
        this.species = species;
        this.name = name;
        this.age = age;
    }

    displayInfo() {
        return `${this.name} is a ${this.species} and is ${this.age} years old.`;
    }

    isAdult(minAge) {
        return this.age >= minAge ? `${this.name} is an adult.` : `${this.name} is not an adult.`;
    }
}

let wildFriend = new Animal("Tiger", "Sheru", 4);
console.log(wildFriend.displayInfo());
console.log(wildFriend.isAdult(3));

Usage of Object:

To create an instance of an object, use the new keyword. This generates a unique instance of the object, ensuring it has its own independent values that won’t be shared with other instances of the same class. After calling new, specify the class you want to instantiate and provide the required values inside the parentheses (). Keep in mind that you must supply all the values in the correct order without skipping any. This ensures proper initialization of the object’s properties.

Calling Methods:

Objects Methods are functions defined within a class that can interact with the object’s data and return useful values. They allow you to perform actions, retrieve properties like speed, size, or image, and display relevant information. To call a method, you reference the object (created from the class and stored in a variable) followed by a dot (.) and the method’s name.

class Boat {
    constructor(maxSpeed, size, color) {
        this.maxSpeed = maxSpeed;
        this.size = size;
        this.color = color;
        this.fuel = 100; // Initialized with full fuel
    }

    drive() {
        this.fuel -= 10; // Decrease fuel by 10 with each drive
        return `The ${this.color} boat is driving at ${this.maxSpeed} knots.`;
    }

    tankLeft() {
        return `Fuel remaining: ${this.fuel}%`;
    }
}

// Creating an instance of the Boat class
let coolBoat = new Boat(100, "Big", "Blue");

// Calling methods on the Boat instance
console.log(coolBoat.drive());
console.log(coolBoat.tankLeft());

Parameters:

Parameters are the inputs you provide when creating an object. They are used to initialize the object’s properties. In the example above, parameters are provided for maxSpeed, size, and color when creating the object. These values are passed inside the parentheses () when instantiating the coolBoat object.

Return Values:

Return values are outputs produced by methods within an object. In the example above, a method returns the amount of fuel left in the boat’s tank. To practice further, you could create an additional method that calculates and returns the distance the boat can travel based on the current fuel level. This approach allows you to retrieve and work with specific data about the object.