01 IntroducingJavaScript
01 IntroducingJavaScript
INTRODUCING JAVASCRIPT
+ Addition
– Subtraction
* Multiplication
/ Division
% Remainder
The Remainder Operator
• The only arithmetic operator that has no direct counterpart in traditional
mathematics is %, which computes the remainder when the first divided
by the second:
14 % 5 returns 4
14 % 7 returns 0
7 % 14 returns 7
• The result of the % operator make intuitive sense only if both operands are
positive integers. The examples in the book do not depend on knowing
how % works with negative numbers or numbers that have fractional
parts.
• The remainder operator turns out to be useful in a surprising number of
programming applications and is well worth a bit of study.
Precedence
• If an expression contains more than one operator, JavaScript
uses precedence rules to determine the evaluation order. The
arithmetic operators have the following relative precedence:
highest
unary -
* / %
+ -
lowest
Thus, JavaScript evaluates unary – operators first, then the
operators *, /, and %, and then the operators + and -.
• Precedence applies only when two operands compete for the
same operator. If the operators are independent, JavaScript
evaluates expressions from left to right. Parentheses may be
used to change the order of operations.
Variables
• The simplest terms that appear in expressions are constant
literals and variables. A variable is a placeholder for a value
that can be updated as the program runs.
• A variable in JavaScript is most easily envisioned as a box
capable of storing a value
answer
42
• Now, we can put some data into it by using the assignment operator =:
Constant Declarations
• It is often useful to give names to values that you don’t intend
to change while the program runs. Such values are called
constants.
• A constant declaration is similar to a variable declaration:
• Variables
declared using const are called “constants”. They cannot be
reassigned. An attempt to do so would cause an error:
Naming Conventions
• In JavaScript, all names must conform to the syntactic rules
for identifiers, which means that the first character must be a
letter and the remaining characters must be letters, digits, or
the underscore character.
• Beyond these rules that apply to all JavaScript names, there
are several conventions that programmers use to make their
identifier names easier to recognize:
– Variable names and function names begin with a lowercase
letter. If a name consists of more than one word, the first letter
in each word is capitalized, as in numberOfStudents. This
convention is called camel case.
– Class names and program names begin with an uppercase letter.
– Constant names are written entirely in uppercase and use the
underscore character to separate words, as in MAX_HEADROOM.
Assignment Statements
• You can change the value of a variable in your program by
using an assignment statement, which has the general form:
variable = expression;
return expression;
function fahrenheitToCelsius(f) {
return 5 / 9 * (f – 32);
}
1. Double quotes: "Hello".
2. Single quotes: 'Hello'.
3. Backticks: `Hello`.
Example of String Type
• Double and single quotes are “simple” quotes. There’s practically no
difference between them in JavaScript.
• Backticks are “extended functionality” quotes. They allow us to embed
variables and expressions into a string by wrapping them in ${…}, for
example:
Concatenation
• One of the most useful operations available for strings is
concatenation, which consists of combining two strings end
to end with no intervening characters.
• Concatenation is built into JavaScript using the + operator.
For example, the expression "ABC" + "DEF" returns the string
"ABCDEF".
• If you use + with numeric operands, it signifies addition. If at
least one of its operands is a string, JavaScript interprets + as
concatenation. It automatically converts the other operand to
a string and concatenates the two strings, so that
"Catch" + -22 "Catch-22"
The “Hello World” Program
• In 1978, Brian Kernighan and
Dennis Ritchie wrote a reference
manual for the C programming 1.1 Getting Started
language, one of the forerunners of The only way to learn a new programming
language is to write programs in it. The first
JavaScript. program to write is the same for all languages:
authors suggest that the first step in This is the big hurdle; to leap over it you have to
be able to create the program text somewhere,
learning any language is to write a compile it, load it, run it, and find out where your
output went. With these mechanical details
simple program that prints the mastered, everything else is comparatively easy.
words “hello, world” on the display. In C, the program to print “hello, world” is
main() {
• The next few slides show you how }
printf("hello, world");
http://mypage/index.html
index.html O
• This file asks the browser to load the file HelloWorld.js and
then call the function HelloWorld once the page is loaded.
Creating the HTML File (Version 2)
• The output from the console log appears in different places in
different browsers and usually requires the user to take some
explicit action before it is visible.
• To make the console log easier to find, we have provided a
library called JSConsole.js that redirects the console log to
an area of the web page marked with the id JSConsole.
<!DOCTYPE html>
<html>
<head>
<title>HelloWorld</title>
<script type="text/javascript" src="JSConsole.js"></script>
<script type="text/javascript" src="HelloWorld.js"></script>
</head>
<body onload="HelloWorld()">
</body>
</html>