JavaScript(JS) Methods
Accessing Object Methods
To access an object method in JavaScript, you can use dot notation to access the method as a property of the object, and then call the method using parentheses. Here's an example:
const person = { firstName: "John", lastName: "Doe", age: 30, fullName: function() { return `${this.firstName} ${this.lastName}`; } }; console.log(person.fullName()); // Output: "John Doe"Source.www:theitroad.com
In this example, we're accessing the fullName
method of the person
object using dot notation, and then calling the method using parentheses. The method returns the person's full name, which we then log to the console using console.log
.
Note that when defining an object method, you can use the this
keyword to refer to the object itself, which allows you to access other properties or methods of the object. In the example above, the fullName
method uses this.firstName
and this.lastName
to access the firstName
and lastName
properties of the person
object.
JavaScript Built-In Methods
JavaScript provides a number of built-in methods that can be used to manipulate data or perform common tasks. Here are some examples of commonly used built-in methods in JavaScript:
String Methods
- `length`: Returns the length of a string. - `charAt(index)`: Returns the character at the specified index in a string. - `concat(str1, str2, ..., strN)`: Concatenates two or more strings. - `indexOf(searchValue, startIndex)`: Returns the index of the first occurrence of a specified value in a string. - `slice(startIndex, endIndex)`: Extracts a section of a string and returns a new string.Here's an example of using some of these string methods:
const str = "hello world"; console.log(str.length); // Output: 11 console.log(str.charAt(0)); // Output: "h" console.log(str.concat("!", " Goodbye!")); // Output: "hello world! Goodbye!" console.log(str.indexOf("o")); // Output: 4 console.log(str.slice(0, 5)); // Output: "hello"
Array Methods
- `length`: Returns the length of an array. - `push(element1, element2, ..., elementN)`: Adds one or more elements to the end of an array. - `pop()`: Removes the last element from an array and returns it. - `join(separator)`: Joins all elements of an array into a string, using a specified separator. - `slice(startIndex, endIndex)`: Extracts a section of an array and returns a new array.Here's an example of using some of these array methods:
const arr = [1, 2, 3]; console.log(arr.length); // Output: 3 arr.push(4); console.log(arr); // Output: [1, 2, 3, 4] console.log(arr.pop()); // Output: 4 console.log(arr.join("-")); // Output: "1-2-3" console.log(arr.slice(1, 3)); // Output: [2, 3]
Math Methods
- `random()`: Returns a random number between 0 and 1. - `floor(num)`: Returns the largest integer less than or equal to a number. - `ceil(num)`: Returns the smallest integer greater than or equal to a number. - `round(num)`: Returns the value of a number rounded to the nearest integer.Here's an example of using some of these math methods:
console.log(Math.random()); // Output: a random number between 0 and 1 console.log(Math.floor(3.7)); // Output: 3 console.log(Math.ceil(3.1)); // Output: 4 console.log(Math.round(3.5)); // Output: 4
These are just a few examples of the built-in methods available in JavaScript. Understanding these and other built-in methods can allow you to perform common tasks more easily and efficiently in your JavaScript code.
Adding a Method to a JavaScript Object
To add a method to a JavaScript object, you can simply define a new property on the object and set its value to a function. Here's an example:
const person = { firstName: "John", lastName: "Doe", age: 30, fullName: function() { return `${this.firstName} ${this.lastName}`; }, getBirthYear: function() { const currentYear = new Date().getFullYear(); return currentYear - this.age; } }; console.log(person.fullName()); // Output: "John Doe" console.log(person.getBirthYear()); // Output: current year - 30 (assuming current year is 2023)
In this example, we've defined a new method getBirthYear
on the person
object. The method calculates the person's birth year based on their age and the current year, and returns the result. We can then call this method on the person
object using dot notation, just like we did with the fullName
method.