Beyond bracket notation, there are many other tools we can use to work with strings. Talking about these tools requires some new terminology.
An object is a collection of related data and operations. An operation that can be carried out on an object is known as a method. A piece of data associated with an object is known as a property.
Suppose we had a
Since a square has four sides of the same length, it should have a property to represent this length. This property could be called
length. For a given square, it will have a specific value, such as 4.
Since a square has an area, it should have a method to calculate the area. This method could be called
area, and it should calculate the area of a square using its
You can think of methods and properties as functions and variables, respectively, that "belong to" an object. Properties and methods are accessed using dot notation, which dictates that we use the object name, followed by a
., followed by the property or method name. When using a method, we must also use parentheses as we do when calling regular functions.
Returning to the
square example, we can access its length by typing
We can calculate the area by calling
area by itself in code does not give you the value of
square.length or carry out the calculation in
square.area(). It does not make sense to refer to a property or method without also referring to the associated object. Typing simply
area() is ambiguous. There might be multiple squares, and it would be unclear which one you were asking about.
We have already encountered one object, the built-in object
console, which we use to output messages.
console is indeed
console.log, we are calling the
log method of the
The fact that strings are objects means that they have associated data and operations, or properties and methods as we will call them from now on.
Every string that we work with will have the same properties and methods. The most useful string property is named
length, and it tells us how many characters are in a string.
1 2 3 4 5
let firstName = "Grace"; let lastName = "Hopper"; console.log(firstName, "has", firstName.length, "characters"); console.log(lastName, "has", lastName.length, "characters");
Grace has 5 characters Hopper has 6 characters
Every string has a
length property, which is an integer.
length property is the only string property that we will use, but there are many useful string methods. We will explore these in depth in the section String Methods, but let's look at one now to give you an idea of what's ahead.
toLowerCase() string method returns the value of its string in all lowercase letters. Since it is a method, we must precede it with a specific string in order to use it.
1 2 3 4
let nonprofit = "LaunchCode"; console.log(nonprofit.toLowerCase()); console.log(nonprofit);
toLowerCase() does not alter the string itself, but instead returns the result of converting the string to all lowercase characters. In fact, it is not possible to alter the characters within a string, as we will now see.
word = 'Rutabaga', why does
word.length return the integer 8, but
What is the length of
cityName = "Vienna"; stateName = "Virginia"; location = cityName + ", " + stateName; console.log(location.length);