Overview of the basic program


In this topic, you will learn how to develop your first JS script. These programs are quite simple yet functional, so they do a good job of showing that programming in JavaScript is easy and enjoyable. However, there are caveats and difficulties everywhere, so we will also look at some common programming errors and how to avoid them.

Hello, World!

When learning any programming language, it is a kind of tradition to start with writing a program that displays the message "Hello, World" on the screen or another device. And who are we to break traditions?

console.log ("Hello, World");

 

You may run the above you should get this result:

Hello, World

As you can see, the script consists of one line and simply prints the text passed in brackets. Note that the quotes are also ignored when outputting the result. This code is very simple but do something a detailed review.

Explanation

  • Here console.log is a function. A function is a block of code that performs useful work for you, such as printing text. In a way, a function is a set of instructions designed to perform a frequently used operation within a program that can be reused in your programs. When a function name is followed by parentheses, it means that it has been called to get the result. 
  • Console.log  allows you to output information to the console, so this function is often used to find errors in the code.
  • "Hello, World" is a string. All strings in JavaScript are enclosed in single or double quotes, so 'Hello, World' would also be a valid string.

Let’s see the following code:

console.log ('Hello, World');

The output of the above program is:

Hello, World

Printing quotes

If you want to include quotes in a string, using two methods print successfully them:

Put a backslash (\) before the quotation marks:

console.log ('Yes, I\'m ready to learn JS in TutorialsProg.');

 

You can quote this line in other types of quotes, for example:

console.log ("Yes, I'm ready to learn JS in TutorialsProg.");

 

The result of both will be as follows:

Yes, I'm ready to learn JS in TutorialsProg.

Possible errors

The error has happened even in simple code lines. The most common type of errors are:

misprints

consoe.log ("Hello, World");

 This line contains consoe.log instead of console.log. This code will not work because of a misprint.

missing one or both quotation marks for a string

console.log (Hello, World");


This does not work because of missing closing quotes.

 

Leave a comment
No Cmomments yet