Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 1, Topic 8
In Progress

JavaScript : Methods

abeerairfan 27 Oct 2021
Lesson Progress
0% Complete

Keep in mind that methods are actions that we can take. JavaScript includes a number of string methods.

We call or use these methods by appending the following to an instance:

  • A period (the dot operator)
  • The name of method
  • Opening and closing parentheses

Syntax: 'string'.methodName();

Isn’t that syntax a little familiar? When we use console.log(), we are calling the console object’s .log() method. Let’s take a look at console.log() and some real-world string methods in action!

console.log('Code'.toUpperCase()); // Prints 'CODE'
console.log('Code'.toLowerCase()); // Prints 'code'
console.log('JavaScript'.startsWith('J')); // Prints true
console.log('JavaScript'.startsWith('C')); // Prints false
  • The .toUpperCase() method is called on the string instance ‘Code’ in the first line. The output is printed to the console. This method returns the string ‘CODE’ in all capital letters.
  • The .toLowerCase() method is called on the string instance ‘Code’ in the second line. The output is printed to the console. This method returns the string ‘code’ in all small letters.
  • On the third line, the .startsWith() method is called on the string instance ‘JavaScript’. This method also accepts the character ‘J’ as an input, or argument, between the parentheses. Because the string ‘JavaScript’ begins with the letter ‘J,’ the method returns the boolean true
  • On the fourth line, the .startsWith() method is called on the string instance ‘JavaScript’. Here because the string ‘JavaScript’ does not begin with the letter ‘C,’ the method returns the boolean false.

A list of built-in string methods can be found in the JavaScript Documentation. Documentation is used by developers as a reference tool. It describes JavaScript’s keywords, methods, and syntax.

%d bloggers like this: