Chapter 4 - JavaScript Introduction To Scripting
Chapter 4 - JavaScript Introduction To Scripting
JavaScript: Introduction to
Scripting
Fig. 6.6 | Prompt dialog displayed by the window object’s prompt method.
4.4.1 Dynamic Welcome Page (cont.)
• A variable is assigned a value with an assignment statement,
using the assignment operator, =.
• The = operator is called a binary operator, because it has two
operands.
String Concatenation
Lines 16–17 use document.writeln to display the new welcome
message.
The expression inside the parentheses uses the operator + to “add” a
string (the literal "<h1>Hello, "), the variable name (the string that the user
entered in line 14) and another string (the literal ", welcome to JavaScript
programming!</h1>").
JavaScript has a version of the + operator for string concatenation that
enables a string and a value of another data type (including another string)
to be combined.
4.4.1 Dynamic Welcome Page (cont.)
• null keyword
Signifies that a variable has no value
null is not a string literal, but rather a predefined term
indicating the absence of value
Writing a null value to the document, however, displays
the word “null”
• Function parseInt
converts its string argument to an integer
• JavaScript has a version of the + operator for string
concatenation that enables a string and a value of another
data type (including another string) to be concatenated
4.4.2 Adding Integers
Fig. 6.9 | Memory locations after inputting values for variables number1 and number2.
Fig. 6.10 | Memory locations after calculating the sum of number1 and number2.
4.5 Memory Concepts (Cont.)
Data Types in JavaScript
• Unlike its predecessor languages C, C++ and Java, JavaScript does not
require variables to have a declared type before they can be used in a script.
A variable in JavaScript can contain a value of any data type, and in many
situations JavaScript automatically converts between values of different
types for you. For this reason, JavaScript is referred to as a loosely typed
language.
• When a variable is declared in JavaScript, but is not given a value, the
variable has an undefined value. Attempting to use the value of such a
variable is normally a logic error.
• When variables are declared, they’re not assigned values unless you specify
them.
• Assigning the value null to a variable indicates that it does not contain a
value.
4.6 Arithmetic
• The basic arithmetic operators (+, -, *, /, and %) are binary
operators, because they each operate on two operands
• JavaScript provides the remainder operator, %, which yields the
remainder after division
• Arithmetic expressions in JavaScript must be written in straight-line
form to facilitate entering programs into the computer