How to write your first JavaScript program?



How to start JavaScript programming? “Hello world!” This is an old tradition in programming and the first code that starts any language and its execution is a good way to ensure the correct configuration of the code.

In this article we will make the first JavaScript programming and thus to make the program more interesting, “Hello, world!” We make the tradition dynamic; So he asks for the username and greets that name. At the end of this article the program “Hello, world!” You will have dynamics.

prerequisites

You can run the code in this tutorial in your web browser using the JavaScript console. Before you start this lesson, you should be familiar with this tool.

Create the first application “Hello, world!” In Java

To type “Hello, world!”, First open the JavaScript console of your web browser. Two main ways in JavaScript to display “Hello, world!” There are:

  1. Warning method ()
  2. Method () console.log.

Use warning ()

The first way we can write this program is to use the alert () method, which displays a warning field with a specific message and a confirmation button. In this method, we send the data type string as a parameter. This string contains the value Hello, World, which is printed in the warning field.

To write the first program, the string “Hello, world!” Put the alert () method in parentheses. In JavaScript, we use a semicolon at the end of a line to complete this line of code:

alert("Hello, World!");

After pressing the Enter key, you should see the following warning in your browser:

Python programming training

Learn the JavaScript programming language

The console prints the code evaluation result, and when the code returns nothing, it is displayed as null.

Pop-up alerts or alert fields can be frustrating; So, let’s run the same console program using console.log ().

Use console.log ()

We can print the same string in the JavaScript console using the console.log () method. Using this method is similar to working with other programming languages ​​in your computer terminal environment.

Like what we warned with (), the string “Hello, world!” In parentheses of the console.log () method and complete our code with a semicolon:

console.log("Hello, World!");

When we press Enter, the message “Hello, world!” Printed on the console:

Hello, World!

In the next section, we’ll teach you how to interact more with this user.

Require prompted user input

Every time the program “Hello, world!” We start alone, we see only the same fixed output. Let’s ask the name of the person running our program. We can then use this name to customize the output.

We use the prompt () method in JavaScript to get the value from the user and, as a question, the string “What is your name?” We send him to ask what his name is. The input that the user enters is then stored in the name of the variable:

let name = prompt("What is your name?");

When you press Enter to start this line, you will receive a pop-up message:

    Basic Java training

Steps for Java programming

The input field that appears in your web browser window contains a text box where the user can enter text. After entering the value, the user must click OK to save the entered value. Also, the user can cancel this operation and not send a login by clicking the Cancel button.
The JavaScript prompt () method is used only when its use in the program is logical and necessary; Because excessive use can be annoying for the user.

In this step, enter the name you want the program to welcome. For this example we use the name Sami.

Now that we have the value of the username, we can use the input value to greet the user.

Greet the user with a () signal

As we explained, the alert () method creates a pop-up window in the browser window. We will use the string link to write the greeting “elloHello! ‌ “, which addresses the user directly. So, let’s paste the string Hello to the variable name:

"Hello, " + name + "!"

We combined the two strings “Hello” and “” and put the name variable between them. We can now send this phrase as a warning ():

alert("Hello, " + name + "!");

When we press Enter here, we see the following field on the screen:

The best tutorial for JavaScript

Java program codes

Then enter the username, Sammy; So, the exit welcomes Sami. Now let’s rewrite this so that the output is printed on the console instead of in the alarm box.

Welcome user with console.log ()

As we saw in the previous section, the console.log () method prints the output to the console; Just like the print () function, which can print output to a terminal in Python. We will use the same continuous string that we used in the alert () method:

"Hello, " + name + "!"

Put the whole phrase in parentheses of the console.log () method and get the username as the output:

console.log("Hello, " + name + "!");

For a user named Sammy, the console output will be as follows:

Hello, Sammy!

We already have a JavaScript program that receives information from the user and prints it on the screen.

Conclusion

JavaScript programming training for beginners is explained in this article. If you’ve been following this content for a while, you can download the classic “Hello, World!” Run with JavaScript and make the application more dynamic and extensive by receiving user input and displaying it as output. In this article we will introduce you to different ways to display text in JavaScript. For example, you can ask the user for the desired color and ask the program to print that color. You can even try using the same technique to create Mad Lib.

Frequently Asked Questions

1. How can we write “Hello World” inside the code?

Using a document. write (Hello world)

2. How to execute JavaScript code in the terminal?

Using Node.js. This can be done. If you have installed it, just open the terminal and enter “node FileName.js”.

3. Where does the JavaScript code run?

This code is usually executed in web browsers.

4. How to execute JavaScript code in the browser?

JavaScript code can be executed by embedding it in HTML files or using it directly in the browser console.

{“@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [{
“@type”: “Question”,
“name”: “چگونه می‌توانیم درون کد “Hello World” را بنویسیم؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “با استفاده از document. write(“Hello World”)”
}
},{
“@type”: “Question”,
“name”: “چگونه کدهای جاوااسکریپت را در ترمینال اجرا کنیم؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “با استفاده از Node.js. می‌توان این کار را انجام داد. اگر آن را نصب کرده‌اید، به‌سادگی ترمینال را باز و “node FileName.js” را تایپ کنید.”
}
},{
“@type”: “Question”,
“name”: “کدهای جاوااسکریپت کجا اجرا می‌شوند؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “این کدها معمولاً در مرورگرهای وب اجرا می‌شوند.”
}
},{
“@type”: “Question”,
“name”: “چگونه کدهای جاوااسکریپت را در مرورگر اجرا کنیم؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “کدهای جاوااسکریپت را می‌توان با قراردادن آن‌ها در فایل‌های HTML یا استفاده مستقیم در Console مرورگر می‌توان اجرا کرد.”
}
}]
}

How to write your first JavaScript program? For the first time in Parsupack. appear.

Leave Your Comment

Your email address will not be published.

Supportscreen tag