THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Javascript Syntax
Javascript (or JavaScript) is a programming language. It’s
syntax has much in common with other major programming
languages. Simply learning Javascript will make learning
additional programming languages significantly easier.
Before we go any further, please understand that Javascript is
NOT the same as Java. They are different programming
languages and have nothing to do with each other.
Java is NOT short for Javascript. It’s a very common
misconception made by code noobz. You are not a code noob.
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
Computer programs are created by programming languages,
like Javascript, and are given specific instructions to perform.
Programming languages give computer programs instructions
through statements.
Statements are separated by semicolons, like so:
var
funMessage
=
“Hello!”;
window.alert(funMessage);
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
Statements in Javascript have five separate elements:
1. Values
2. Keywords
3. Expressions
4. Operators
5. Comments
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
VALUES
There are two types of values in Javascript:
Fixed
Variable
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
VALUES
LITERALS
Fixed Values (a.k.a. Literals)
Think of these values as something that doesn’t change. Ever.
Because it’s been “hard coded”.
Numbers such as 17.425
or
946
Strings are simply text, written in ‘single’ or “double
quotes”
Expressions compute to values, like math: 10
*
4
or
3.14
–
6
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
VALUES
VARIABLES
Variable Values (a.k.a. Variables)
Variables are different from literals because the value can
change. Variables are used to store data, such as numbers,
strings, or expressions. Think of a variable as a bucket that can
have stuff in it.
Javascript uses the keyword var
to define a variable, then
uses = to assign a value to the variable.
var
myVariable;
myVariable
=
2
*
4;
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
OPERATORS
In Javascript, and many other programming languages, there
are many different types of operators. Two of the most
common operators are:
Arithmetic Operators
Assignment Operators
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
OPERATORS
ARITHMETIC
Arithmetic operators are used for math! Here are some of the
most common arithmetic operators:
Operator Description Example
+ Addition var
a
=
1
+
2;
-‐ Subtraction var
b
=
10
-‐
8;
* Multiplication var
c
=
a
*
b;
/ Division var
d
=
100
/
c;
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
OPERATORS
ASSIGNMENT
Assignment operators are used to assign values to variables.
Operator Example Is the same as What it does
= a
=
b a
=
b Assigns a value to a variable
+= a
+=
b a
=
a
+
b Adds a value to a variable
-‐= a
-‐=
b a
=
a
-‐
b Subtracts a value to a variable
*= a
*=
b a
=
a
*
b Multiplies a value to a variable
/= a
/=
b a
=
a
/
b Divides a value to a variable
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Statements
COMMENTS
You can hide statements from the computer program using
comments, so the statements do not execute. There are two
types of comments in Javascript:
//
Single
line
comments
/*
Multi
line
comments
that
can
take
up
as
many
lines
as
you
want
*/
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY
Case Sensitive
Javascript is case sensitive, meaning the following variables
are entirely different:
yourName
=
“Brad
Hussey”;
yourname
=
“Coding
Cody”;
That means the keyword var must always be typed in
lowercase.
end();
THE BASIC SYNTAX OF JAVASCRIPT
THE ULTIMATE WEB DESIGNER • BRAD HUSSEY