Javascript
Javascript
Chapter One:
JavaScript Statements
JS Syntax
JS Comments
JS Variables
JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments, most
JavaScript programs contain many JavaScript statements. The statements are executed, one by one, in the
same order as they are written.
Semicolons separate JavaScript statements, add a semicolon at the end of each executable statement, when
separated by semicolons, multiple statements on one line are allowed, ending statements with semicolon is
not required, but highly recommended.
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.
For best readability, programmers often like to avoid code lines longer than 80 characters. If a JavaScript
statement does not fit on one line, the best place to break it, is after an operator.
JavaScript statements can be grouped together in code blocks, inside curly brackets {...}. The purpose of
code blocks is to define statements to be executed together.
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.
JavaScript syntax is the set of rules, how JavaScript programs are constructed.
In a programming language, variables are used to store data values. JavaScript uses the “var” keyword to
declare variables.
JavaScript uses an assignment operator = to assign values to variables.
JavaScript uses arithmetic operators (+ - * /) to compute values.
An expression is a combination of values, variables, and operators, which computes to a value.
The computation is called an evaluation, for example, 5 * 10 evaluates to 50.
Not all statements are "executed", code after double slashes // or between /* and */ is treated as a
comment, comments are ignored, and will not be executed.
Identifiers are used to name variables (and keywords, and functions, and labels). The rules for legal names
are much the same in most programming languages. In JavaScript, the first character must be a letter, or an
underscore (_), or a dollar sign ($), subsequent characters may be letters, digits, underscores, or dollar signs.
Numbers are not allowed as the first character.
All JavaScript identifiers are case sensitive, the variables lastName and lastname, are two different variables.
Hyphens (master-card) are not allowed in JavaScript. They are reserved for subtractions.
JavaScript programmers tend to use camel case that starts with a lowercase letter: firstName, lastName,
masterCard, interCity.
A variable without a value, has the value undefined, null is an empty or non-existent value.
In JavaScript we can use these codes to display output on screen
1. Writing into an HTML element, using innerHTML.
2. Writing into the HTML output using document.write().
3. Writing into an alert box, using window.alert().
4. Writing into the browser console, using console.log().
Page 2 of 8
Logical operators are used to determine the logic between variables or values, given that x = 6 and y = 3, the
table below explains the logical operators.
Page 3 of 8
JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32
bits binary numbers, before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed
integers, after the bitwise operation is performed, the result is converted back to 64 bits JavaScript numbers.
Page 4 of 8
Chapter Four:
JavaScript Data Types
In JavaScript there are 5 different data types that can contain values (string, number, Boolean, object,
function)
Page 5 of 8
JavaScript has dynamic types. This means that the same variable can be used to hold different data types for
example
A string (or a text string) is a series of characters like "John Doe", strings are written with quotes. You can use
single or double quotes.
JavaScript has only one type of numbers, numbers can be written with, or without decimals
Booleans can only have two values: true or false, Booleans are often used in conditional testing.
Arrays are written with square brackets, array items are separated by commas, Array indexes are zero-
based, which means the first item is [0], second is [1], and so on, the following code declares (creates) an
array called cars, containing three items (car names)
Objects are variables too. But objects can contain many values, JavaScript objects are written with curly
braces, object properties are written as name: value pairs, separated by commas.
typeof operator can be used to find the data type of a JavaScript variable.
Page 6 of 8
Chapter Five:
JS Functions
A function is a block of code designed to perform a particular task, it is executed when "something" invokes
it (calls it).
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (),
the parentheses may include parameter names separated by commas (parameter01, parameter02, so on),
function parameters are listed inside the parentheses () in the function definition.
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables), the code
to be executed, by the function, is placed inside curly brackets: {}
Function arguments are the values received by the function when it is invoked, inside the function, the
arguments (the parameters) behave as local variables.
The code inside the function will execute when "something" invokes (calls) the function:
o When an event occurs (when a user clicks a button)
o When it is invoked (called) from JavaScript code
o Automatically (self-invoked)
When JavaScript reaches a return statement, the function will stop executing, If the function was invoked
from a statement, JavaScript will "return" to execute the code after the invoking statement, functions often
compute a return value, the return value is "returned" back to the "caller"
Variables declared within a JavaScript function, become LOCAL to the function, local variables can only be
accessed from within the function.
Page 7 of 8
Objects can also have methods. Methods are actions that can be performed on objects. A method is a
function stored as a property.
Page 8 of 8