Dot operator in JavaScript | Example code

  • by

The JavaScript dot (.) operator is simply an operator that sits between its operands, just like + and -. The variables stored in an object that we access via the dot operator are generically called properties.

object.property 

JavaScript’s object support is the notion of chaining dots together to dive down into complex structures.

Dot operator in JavaScript

Simple example code access the object property using dot notation.

<!DOCTYPE html>
<html>
<body>

  <script>

    function Student(name, age, standard) {
      this.name = name;
      this.age = age;
      this.standard = standard;
    }
    let student = new Student("Rohan", 18, 12);

    console.log(student.name,student.age)
  </script>

</body>
</html> 

Output:

Dot operator in JavaScript

Using dot operator with functions

Functions are objects, and they can have properties just like any other object.

var t = function() {
    console.log('foo');
};

t.bar = function() {
    console.log('bar');
};

t.bar();

t.favColor = 'red';
t.shoeSize = 12;

Output: bar

Do comment if you have any doubts or suggestions on this JS operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.