please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 3 Next
 

3. Variables and Data Types

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.

3.1 Variables

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;

Variable Declaration

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.

Untyped Variables

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";
In C, C++, or Java, these lines of code would be illegal.

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
As we've seen, we just use the var keyword to declare variable in JavaScript, with no need to specify a type:

var i;  // a declaration of an untyped JavaScript variable.
In fact, although it is good programming style to declare variables in JavaScript, it is usually unnecessary, precisely because JavaScript is untyped.

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.


Previous Home Next
Reserved Words Book Index Numbers

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch03_01.htm, 9KB, , updated: 2019/10/14 15:00, local time: 2024/11/8 09:57,
TOP NEW HELP FIND: 
3.149.251.104:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/Techref/language/java/script/definitive/ch03_01.htm"> [Chapter 3] Variables and Data Types</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?