JavaScript Basics #7: Methods & Properties

JavaScript Basics #7: Methods & Properties
Jan Zavrel
Jan Zavrel Follow July 01, 2021
Share:

Let’s focus on the console.log() for a bit now. The log() part is a method, which is a special kind of function. We will discuss functions and methods later, but since we use this one so frequently, I wanted you to know in the advance what it is.

Each time you create a new piece of data in a JavaScript program, the browser saves it as an instance of the data type. An instance can have both methods and properties depending on its declaration.

The . in front of the log() is another operator and we call it the dot operator. It allows us to access the methods and properties of the instances.

Methods

You can easily recognize methods because they always start with dot operator, followed by the name of the method and parentheses.

let myName = 'Jan';
console.log(myName.toUpperCase()); //Prints JAN

In the example above, we use the toUpperCase() method to modify the instance of the string 'Jan' stored in the variable myName. The program prints JAN to the console because that’s the upper case version of it.

Properties

Unlike methods, properties don’t have the parenthesis at the end of the name. For example, strings have a length property that tells how many characters there are in the string.

console.log(myName.length); //Prints 3

In the example above, the value saved to the length property is retrieved from the instance of the string 'Jan' stored in the variable myName. The program prints 3 to the console, because Jan has three characters in it.

Don’t worry if this is complicated or confusing for you. We’ll get back to it later in more details. Just remember that there’s the dot operator that gives us the access to methods and properties. That’s all you need to know for now.

Follow me on Medium, Twitter, Facebook and YouTube.

Share:
Jan Zavrel
Written by

Jan Zavrel Follow

Developer, Author, Teacher, Evernote Certified Consultant.