tailieunhanh - Simply JavaScript phần 4

Nó cho biết thêm hai con số. Nếu chúng ta đang nhận được những con số khi chúng ta chạy chương trình, chúng tôi không biết những gì họ sẽ có khi chúng ta viết chương trình, vì vậy chúng tôi cần một số cách đề cập đến họ mà không cần sử dụng con số thực tế. Làm thế nào về chúng tôi cung cấp cho chúng những cái tên? | 20 Simply JavaScript It adds two numbers. If we re getting those numbers when we run the program we don t know what they ll be when we write the program so we need some way of referring to them without using actual numbers. How about we give them names Say . x and y. Using those names we could rewrite the program as x y Then when we get our data values from some faraway place we just need to make sure it s called x and y. Once we ve done that we ve got variables. Variables allow us to give a piece of data a name then reference that data by its name further along in our program. This way we can reuse a piece of data without having to remember what its actual value was all we have to do is remember a variable name. In JavaScript we create a variable by using the keyword var and specifying the name we want to use var chameleon This is called declaring a variable. Having been declared chameleon is ready to have some data assigned to it. We can do this using the assignment operator placing the variable name on the left and the data on the right var chameleon chameleon blue This whole process can be shortened by declaring and assigning the variable in one go var chameleon blue Simply JavaScript Programming with JavaScript 21 In practice this is what most JavaScript programmers do declare a variable whenever that variable is first assigned some data. If you ve never referenced a particular variable name before you can actually assign that variable without declaring it using var chameleon blue The JavaScript interpreter will detect that this variable hasn t been declared before and will automatically declare it when you try to assign a value to it. At first glance this statement seems to do exactly the same thing as using the var keyword however the variable that it declares is actually quite different as we ll see later in this chapter when we discuss functions and scoping. For now take it from me it s always safest to use var. The var keyword has to be

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.