Understand Properties and Methods
Properties & Methods
Strings and numbers have special properties and methods. By the end of this section we will learn about Math, arrays, and objects which also have properties and methods.
Property
A property can give you information about a piece of data - like the length of a string. A property can also give you relevant data - like the number pi from the Math keyword.
Property Syntax
To use a property you use either the data (ie. 'Jeremy') or a keyword (ie. Math) followed by a dot (.) then the name of the property (ie. length).
console.log('Jeremy'.length); // prints 6console.log(Number.NEGATIVE_INFINITY); // prints -INFINITYconsole.log(Math.PI); // prints 3.141592653589793...
Method
Methods use the data and return a result. For instance, the toUpperCase()
method returns the string in all upper case letters.
Method Syntax
The method syntax is the same as the property syntax only methods end with parentheses: (). Inside the parentheses can be 0 or more arguments.
console.log('Jeremy'.toUpperCase()); // prints "JEREMY"console.log(437983 .toExponential()); // prints "4.37983e+5"console.log(Math.round(6.8414)); // prints 7console.log('Hello'.concat(' everyone, ', 'I hope', ' you are doing well!')); // prints "Hello everyone, I hope you are doing well!"
Arguments/Parameters
Arguments also called parameters are information used to get specific things back from the method. You place arguments inside the parentheses. Multiple arguments are separated by commas.
// Zero arguments in the toUpperCase() string method.console.log('Jeremy'.toUpperCase());// One argument in the round() math method.console.log(Math.round(6.8414));// The concat() string method allows multiple arguments.console.log('Hello'.concat(', everyone, ', 'I hope', ' you are doing well!'));