官术网_书友最值得收藏!

  • The JavaScript Workshop
  • Joseph Labrecque Jahred Love Daniel Rosenbaum Nick Turner Gaurav Mehla Alonzo L. Hosford Florian Sloot Philip Kirkbride
  • 620字
  • 2021-06-11 12:52:53

An Introduction to Variables

In almost any language, JavaScript included, the first step to programming is to understand the common variable. A variable can be thought of as an identifier for a certain piece of data. To declare a variable in JavaScript, we use the reserved word var:

var name;

In the preceding example, we declare a variable with the name identifier. Our variable does not have any data associated with it yet. For that, we must use an assignment operator:

name = "Joseph";

Since the variable name has already been declared, we no longer need to use var to declare it in this second step. We simply address the variable by its name and then follow that with an assignment operator of = and then a value, in this case, "Joseph". Of course, you will likely want to use your own name here.

We terminate each line of code with a ; for convention and readability. Note that we can also perform the variable declaration and assignment in a single line of code:

var name = "Joseph";

You now know the foundations of how to declare and assign data values to a variable.

Exercise 1.03: Programming First Steps

Let's go ahead and step through a few bits of JavaScript code within the developer tools console before moving on. If you have your browser developer tools still open from the preceding section. If not, refer to the Accessing Web Browser Developer Tools section of this chapter to access the console.

With the console now available within the web browser, we'll step through a few basic JavaScript declarations:

  1. Within the console, type in the following code and hit Enter:

    var myCity= "London";

    This declares a variable with the identifying name of myCity. This will allow you to invoke this variable later on.

  2. Since this variable is now defined in memory, we can address it whenever we like. Type the following within the console and hit Enter:

    alert("Welcome to " + myCity + "!");

    An alert will pop up over the browser viewport stating, "Welcome to London!". To achieve the full greeting, we will also add additional string information to the variable using concatenation with the + operator. This allows us to mix variable values and plain text data together in our output.

Now, you know how to write values to a named variable and how to read those values out by using the variable name.

Activity 1.01: Creating an Alert Box Popup in the Web Browser

In this activity, you will call JavaScript and witness its tight relationship to the web browser. You will learn how to execute an alert within the web browser environment using the browser developer tools.

Note

We'll be using Google Chrome for the following instructions and output images. Other browsers will differ slightly.

Steps:

  1. Press F12 to open the developer tools. Alternatively, a right-click may expose a menu from which you can select Inspect.
  2. Activate the Console tab. The developer tools may default to this view. If not, there is likely a Console tab you can click on to activate it.
  3. Within the console, write the JavaScript command.
  4. Hit Return/Enter to execute the code.

Expected output:

The output should be similar to this:

Figure 1.12: An alert appears with our message

Note

The solution to this activity can be found on page 710.

In this section, we had a look at how to access the web browser developer tools across a variety of popular browsers and had a look at some of the different views that can be accessed within.

In the next section, we'll get an overview of exactly what JavaScript is used for and take a general look at the capabilities of the language.

主站蜘蛛池模板: 镇赉县| 巴林左旗| 阿图什市| 南川市| 漯河市| 泰兴市| 丹寨县| 长子县| 中方县| 临西县| 同江市| 玉林市| 吕梁市| 昭平县| 观塘区| 翼城县| 抚顺县| 会理县| 安龙县| 霍林郭勒市| 吐鲁番市| 榆社县| 诸城市| 双桥区| 广安市| 比如县| 阳城县| 南涧| 任丘市| 北辰区| 北京市| 宽城| 安西县| 临沂市| 福州市| 新绛县| 扶沟县| 灌南县| 大宁县| 武隆县| 石景山区|