Vous êtes sur la page 1sur 8

Page 1 of 8

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

Exercise: Chapter One


1. Create a variable called carName, assign the value "Volvo" to it, and display it.
2. Create a variable called number, assign the value 50 to it, and display it.
3. The code below should display "Volvo" - Fix it.
4. Display the sum of 5 + 10, using two variables x and y.
5. Create a third variable called z, assign x + y to it, and display it.
6. Use a single var keyword to create three variables with the following values:
firstName = "John"
lastName = "Doe"
age = 35
Chapter Two:
 JavaScript Operators
 Arithmetic operators are used to perform arithmetic on numbers.

 The + operator can also be used to add (concatenate) strings.


 Adding two numbers, will return the sum, but adding a number and a string will return a string.
 Comparison operators can be used in conditional statements to compare values and take action depending
on the result:

 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

Exercise Chapter Two


1. Display the sum of 10 + 5, using two variables x and y.
2. Display the difference of 10 - 5, using two variables x and y.
3. Display the product of 10 * 5, using two variables x and y.
4. Display the result of 10 / 5, using two variables x and y.
5. Find the remainder when 15 is divided by 9, using two variables x and y.
Chapter Three:
 JavaScript Assignment
 Assignment operators assign values to JavaScript variables.

 The += assignment operator can also be used to add (concatenate) strings.

Exercise Chapter #03


1. Use the += operator to add a value of 5 to the variable x.
2. Use the -= operator to subtract a value of 5 from the variable x.
3. Use the *= operator to multiply the variable x with 5.
4. Use the /= operator to divide the variable x with 5.
5. Use the %= operator to assign a remainder of 10 / 3 to the variable x.

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

Exercise Chapter #05


1. Call the function.
2. Figure out what is wrong with the function - fix it and run it as it should!
3. Use the function to display the product of 5 * 5.
4. Use the function to display "Hello John"
5. Define a function named "myFunction", and make it display "Hello World!" in the <p> element.
Chapter #06:
 JS Objects
 An object is something that has describable characteristics that you can affect, and that behaves in a
particular way, an object in the object-oriented programming paradigm is a combination of code and data
that exhibits characteristics and behavior in a similar manner. Objects have properties—defined as their
attributes. Just as objects can have properties, they can also have methods. Methods define the way an
object behaves.
 In real life, a car is an object. A car has properties like weight and color, and methods like start and stop.
 You define (and create) a JavaScript object with an object literal: Spaces and line breaks are not important.
An object definition can span multiple lines:

 You can access object properties in two ways:

 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

 You access an object method with the following syntax:


If you access a method without the () parentheses, it will return the function definition:

Exercise Chapter #06


1. Display "John" by extracting information from the person object.
2. Add the following property and value to the person object: country: USA
3. Create an object called person with name = John, age = 50. Then, access the object to display "John is 50
years old".
Chapter #07:
 JavaScript Events
 An HTML event can be something the browser does, or something a user does.
 HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

 Here is a list of some common HTML events:

Exercise Chapter #07


1. The <p> element should do something when someone clicks on it.
2. When the button is clicked, trigger myFunction() with an event.
3. The <span> element should do something when someone moves the mouse over it.