Contents:
Variables
Numbers
Strings
boolean Values
Functions
Objects
Arrays
Null
Undefined
The Date Object
Data Type Wrapper Objects
This chapter introduces two of the most important concepts of programming languages: variables and data types. A variable is a name associated with a data value; we say that the variable "stores" or "contains" the value. Variables allow us to store and manipulate data in our programs.
Just as fundamental as variables are data types. These, as the name suggests, are the types of data that our programs can manipulate. In Chapter 2, Lexical Structure, we saw that we can include numeric, string, and Boolean literals directly in our programs. This chapter provides more detail about these data types, and also introduces three new ones: functions, objects, and arrays.[1] Later chapters of the book will provide much more detail about functions, objects, and arrays.
[1] Technically, objects and arrays are actually two distinct uses of a single data type. Because they are used in such distinct ways, we will usually consider them as separate types in this book.
In Chapter 2, Lexical Structure, we considered JavaScript literals: constant values embedded directly (or literally) into a JavaScript program. A program that operated only on constant, literal values would not be a very interesting one, and so JavaScript (and all programming languages) use variables. Variables are names that have values assigned to them. They provide a way to manipulate values by name. The value associated with a name need not be constant; new values may be assigned to existing names. Since the value associated with a name may vary, the names are called variables. For example, the following line of JavaScript assigns the value 2 to a variable named i.
i =index.html 2;
And the following line adds 3 to i and assigns the result to a new variable sum:
sum =index.html i + 3;
Although it is often unnecessary, it is good programming style to declare variables before using them. You do this with the var keyword, like this:
var i; var sum;
You can also declare multiple variables with the same var keyword:
var i, sum;
And you can combine variable declaration with initial assignment to the variable:
var i =index.html 2;
As mentioned above, however, variable declaration is not usually required. The first time you use a variable that is not already declared, it will automatically be declared. The only time you actually need to declare a variable with var is when declaring a local variable inside a function definition (we haven't introduced functions yet) and that variable name is also in use as a "global" variable outside of the function. If you simply used the variable in the function without declaring it, then JavaScript would assume you meant the global variable declared outside the function, and would not automatically declare a local one within the function.
An important difference between JavaScript and languages like Java and C is that JavaScript is untyped. This means, in part, that variables can hold values of any data type, unlike Java and C variables which can only hold one type of data. For example, it is perfectly legal in JavaScript to assign a number to a variable and later assign a string to it:
i =index.html 10; i = "ten";
A related implication of the fact that JavaScript is an untyped language is that variable declarations do not have to specify a data type for the variable as they do in C, C++, and Java. In those languages, you declare a variable by specifying the name of the data type it will hold and following that by the variable:
int i; // a declaration of an integer variable in C, C++, or Java
var i; // a declaration of an untyped JavaScript variable.
Another feature of JavaScript's lack of typing is that values are conveniently and automatically converted from one type to another. If you attempt to append a number to a string, for example, JavaScript will automatically convert the number to the corresponding string so that it can be appended. We'll see more about data type conversion in Chapter 9, Further Topics in JavaScript.
JavaScript is obviously a simpler language for being untyped. The advantage of typed languages, like C++ and Java, is that they enforce rigorous programming, and therefore make it easier to write, maintain, and reuse long, complex programs. Since most JavaScript programs are shorter "scripts," this rigor is not necessary, and we benefit from the simpler syntax.
file: /Techref/language/java/script/definitive/ch03_01.htm, 9KB, , updated: 2019/10/14 15:00, local time: 2024/12/22 12:24,
18.217.10.152:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/java/script/definitive/ch03_01.htm"> [Chapter 3] Variables and Data Types</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.