Tuesday, 8 April 2014

Core Language

Chapter 3: Data Types and Variables,,


Although JavaScript was primarily intended to be used to manipulate text in the form of HTML
Web pages within a browser, the data types it offers go well beyond what would be required for
the task. Present in JavaScript are most—if not all—of the data types you‘d find in other
modern scripting languages, as well as a robust set of features with which to manipulate them.
The basic types JavaScript supports are numbers, strings, and Booleans. More complex types
such as objects, arrays, and functions are also part of the language. This chapter covers in
detail the basic data types and their usage. Functions and composite types, such as objects,
are also briefly introduced, but a complete exposition of their capabilities is reserved for
Chapters 5 and 6.
Key Concepts
A variable can be thought of as a container that holds data. It‘s called a ―variable‖ because the
data it contains—its value—varies depending on your script. For example, you might place the
total price of items a customer is buying in a variable, and then add tax to this amount, storing
the result back in the variable. The type of a variable describes the nature of the data stored.
For example, the type of a variable holding the value 3.14 would be number while the type of a
variable holding a sentence would be string. Note that ―string‖ is programming language lingo
for a sequence of characters—in other words, some text.
Since you need to have some way to refer to variables, each variable is given an identifier, a
name that refers to the container and allows the script to access and manipulate the data it
contains. Not surprisingly, a variable‘s identifier is often referred to as its name. When scripts
are run, the JavaScript interpreter (the facility within the browser that executes JavaScript)
needs to allocate space in memory to store a variable‘s value. Declaring a variable is the
process of telling the interpreter to get ready to store data in a new variable. In JavaScript,
variables are declared using the var keyword with the name of the variable you wish to declare.
For example, you might write
var firstName;
You can now store data in the variable known by the identifier firstName. Presumably, you‘d be
storing a string here. We could then assign a value like "Thomas" to the variable. We call the
string "Thomas" a literal, which describes any data appearing directly in the source code. The
complete example is now
var firstName;
firstName = "Thomas";
The illustration here demonstrates all the terms used so far together.





 Although it is good programming practice to declare variables before use, JavaScript allows the
implicit declaration of variables by using them on the left-hand side of an assignment. That is,
when the interpreter sees that a script would likely stick data into a variable that hasn‘t been
declared, it automatically allocates space for the variable without the programmer having to use
the var keyword. For example, you might just assign a variable, like so:
lastName = "Schneider";
Many programmers use this type of implicit declaration to save time when coding. It‘s faster and
easier to not bother declaring variables before using them. Unfortunately, it‘s also not a good
idea. Scripts written without variable declarations are significantly harder to read than those that
use explicit declarations. Implicit declaration can also lead to subtle, hard-to-find errors
involving variable scope, a topic we‘ll discuss later in the chapter. Unless you‘re writing a very
simple script (less than a dozen lines), always explicitly declare your variables.




"news powered by" 
 

Share:

No comments:

Post a Comment

© World Of Programmers All rights reserved | Theme Designed by Seo Blogger Templates