JavaScript Object Methods

JavascriptWeb DevelopmentObject Oriented Programming

JavaScript Object Methods are the actions that can be performed on objects.

Following is the code showing methods present in object in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .sample {
      font-size: 18px;
      font-weight: 500;
      color: red;
<h1>JavaScript Object Methods</h1>
Enter your Name <input type="text" class="name" /> Enter your Age
<input class="age" type="number" />
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to see if you are a legal adult or not
   let sampleEle = document.querySelector(".sample");
   let nameEle = document.querySelector(".name");
   let AgeEle = document.querySelector(".age");
   document.querySelector(".Btn").addEventListener("click", () => {
      let testObj = {name: nameEle.value, age: AgeEle.value,checkAdult() {
            if (this.age >= 18) {
               sampleEle.innerHTML = + " you are a legal adult ";
         } else
            sampleEle.innerHTML = + "you are not a legal adult ";


On entering the name and age and clicking on ‘CLICK HERE’ button −

Published on 11-May-2020 17:30:36