This lesson is just a quick heads up on a type of very common syntax in JavaScript. It involves some concepts that you have not covered yet, but will shortly.
You will definitely come across this syntax at some point:
document.getElementById("button");
What exactly happening is here? It looks like a function but why have a document and then a full stop separating document and the function?
Property Access Dot Notation
The period between an object and its property in JavaScript is known as the "dot operator" or "property access dot notation". It's specifically used as the syntax for accessing an object's properties directly. Many languages have a very similar notation.
For example, if you have an object car with a property color, you would access the color property by writing car.color. In the example given in the previous section, the object is document and the property is getElementById.
In the case of getElementById, since this is a callable property like a function, it's technically termed a method, which you'll cover in more detail in the next section.
This dot notation is one of the ways to interact with objects and their properties in JavaScript, allowing you to read, update, or add new properties to an object.
JavaScript Built-In Methods
In JavaScript, objects, including certain data types like strings, can have properties that are functions. These functions are termed 'methods' when they are part of an object's properties. Like standalone functions, methods are accessed using an object and typically operate on the object's properties. JavaScript provides many built-in methods for these data types, examples of which you'll encounter in future lessons.
Methods can be called with or without parameters, and while some methods operate directly on their parent object without needing explicit parameters, many others do require parameters to specify how they should act on the object. For example:
let greet = "hello world!";
let shout = greet.toUpperCase();
console.log(shout);
Try it out on the console to see what it does!
It might seem surprising that a method can be called on a string. In JavaScript, primitive data types like strings can be treated as objects when calling methods on them. This is because JavaScript automatically 'wraps' these primitives in object-like wrappers to provide access to methods. Further details on this behavior will be covered later in the course.
Summary: Introduction To JavaScript Methods
- Methods are functions that are associated with objects
- JavaScript has many built-in methods
- Built-in methods refers to methods that are predefined