Lesson 5 Methods in JavaScript

In this lesson, we are going to introduce JavaScript methods and we are going to use three built-in methods.

Methods

  1. ALERT
  2. PROPMT
  3. CONSOLE.log

Comments in JavaScript

So before starting JavaScript methods, one thing I want to show you here is JavaScript comments just like CSS and HTML, JavaScript has its own way of defining comments which is just notes and things that don’t actually run and not executed.
So we use double slashes “//” and then whatever I put after double slashes won’t actually work.
img1 So if I did something like 10+10 would be normally 20, but it just does not run it all.
So that’s comments in JavaScript.

clear()

I am also going to show you one more thing here which is clear my console method, type the word clear and follow it with open and close parentheses like this clear(), and hit enter, and all it will do is clear my console.
So clear itself is something called a method.
img2

And when we hit enter, it will clear my console.
You can see a complete blank console in the below screenshot
img3

ALERT Method

So the first method we are going to touch on is called alert(), and alerts is actually a little bit noxious.
What it does, it pops up a message to the user.
It basically alert the user about something.
The syntax of the alert method is below:
Syntax: alert( ) ; Inside of the parentheses, we pass in something that we want to be alerted.
So, I can put a message as a string like “This is Alert Message!!”
img4

So when I hit enter, I get annoying popup message says:
img5

We can also do the mathematical operation in the alert method, you can see here in below example where we have declared two variable num and num1 then multiply and show in the alert method.
img6

Console.log Method

So the next one I want to show is similar to ALERT, but this will do is print something to JavaScript console.
So if someone doesn’t have the console open, they will never see it print.
Here is the syntax of this console.log method below Syntax: console.log ( ); If I want to print something, we need to write console.log() and then write something inside parenthesis with opening and closing quotes.
img7

So, you can notice here console.log() print up ‘Hello from the Asgard’ text.

PROMPT Method

The last method we are going to discuss here is Prompt and Prompt is really useful for us because it lets us get input from the user. So here is the syntax for Prompt method below.
Syntax : prompt ( ) ; So here is an example of prompt method img8

And when you hit enter you will see a pop up asking for something from the user.
img9

And when we write the name and hit enter then we will get the name back in the console.
img10

It does not do anything with name value ‘thor’ because we are not storing the values.
In this part of prompt we are going to tell you that how you can store a value with prompt, here we are going to declare a variable ‘userName’ and then going to store the value in this variable, now I am asking for some input from the user again.
img11

Now a popup will show again and here you will write name: img12

Hit enter and then if I write userName , I can get the username Loki that had been stored this time.
img13

So we use prompt in JavaScript when we want some input from users, and you can either click ‘OK’ or ‘Cancel’ to proceed when prompt dialog box pop up.
In this lesson, we have introduced JavaScript methods and we have studied how you can use ALERT(), PROPMT() and CONSOLE.log() method.

>Continue to Lesson 6

Javascript Lessons for beginners

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?