About this document Variables - Explained (cont) Objects - Explained
This cheat-sheet explains the most Why use variables? What is an object?
important parts of the JavaScript language,
Use them to "remember" things in the program. If a variable is a "box" which can hold a
defines some key terms and shows the
Sometimes, the collection of all variables value, then an object is a box of boxes,
syntax through small examples.
(everything the program remembers) is called the holding many values - each of which is a
However, it's no substitute for proper
state of the program. property.
studying - you can't learn to program off of
a cheat sheet (sorry!). Where to read more What is a property?
Read the section "Variables" at: A property is some small part of an object
Variables - Explained eloquentjavascript.net/02_program_structure.html which holds some data (e.g. string ) or a
Function. Each property has an
What is a variable?
Variables - Examples identifier, just like variables.
A variable is a storage location, a
"box" , which we associate with a name Define a variable Where to read more
(an identif ier). The variable can hold a eloquentjavascript.net/04_data.html - The
var name = "Adalina";
single value and its value may be introduction and the paragraphs
NB - subsequent examples assume we have
changed "Properties" and "Objects"
defined this variable.
What is an identifier?
Retrieve the variable's value
It's the "name" affixed the variable. Later Objects - Examples
Simply refer to the variable's identifier:
on, whether updating or retrieving its Define an object
console.log(name);
value, we'll use refer to the variable by
its identif ier. is (in this case) the same as: Define an object with two properties whose
console.log("Adalina"); identifiers are "name" and "species":
What can a variable hold? var my_pet = {
Update the variable's value
Any string, number, boolean , name: "spot",
array, object or Function. name = "Emma"; species: "dog"
NB - The syntax is the same as defining the }
variable, sans the var keyword!
NB Subsequent examples will assume we
start with this object.
NB It isn't necessary to define a variable to
hold the array (but you almost always will).
By pseud Published 9th March, 2016. Sponsored by CrosswordCheats.com
Last updated 11th March, 2016. Learn to solve cryptic crosswords!
Page 1 of 6. http://crosswordcheats.com
, JavaScript Survival Kit Cheat Sheet
Objects - Examples (cont) Comparisons (cont) Conditions - False & True (cont)
Retrieve a property x > y true if x is greater than y` Where are conditions used?
Get the value of the name property: x < y true if x is less than y Conditions determine which code block to
my_pet["name"] evaluate in if-statements and when to
!x true if x is false
or terminate a loop.
x && true if both x and y are true
my_pet.name
y Functions - Explained
Update a property
x || true if either (or both) x or y are What is a function?
To change the value of the name property
y true
Functions group code together into a block
(i.e. rename our pet):
which is given a name (an identifier).
my_pet["name"] = "sparky";
Conditions - False & True Functions often accept arguments to modify
or
their behaviour.
my_pet.name = "sparky"; What's a condition?
A condition is really just an expression. What is an argument?
Add a property
When we use an expression as a condition, Think of function arguments as variables
my_pet["breed"] = 'bulldog'; we're not interested in its value, but whether which are defined & available to the code
or or not that value is truthy. inside the function. The value of an
my_pet.breed = 'bulldog'; argument is determined by the point the
What's a truthy value?
NB adding/updating a property uses the function is called and the argument(s) is
In JavaScript, all but 6 values are truthy , supplied.
same syntax - if the property didn't exist, it is
added. that is, unless your condition evaluates to
Why use functions?
one of those 6 values, the code guarded by
Remove a property the if-block will be run. Functions are the primary way of defining
To remove the species property: more complex or specific actions than is
What are the falsy values?
delete my_pet["species"]; built into JavaScript and to organise code.
These 6 values will cause the condition to In other words - functions are handy when
or
fail and the code it guards to be skipped: we wish to use a piece of code more than
delete my_pet.species;
• false once.
• 0 - (the number zero)
Where to read more
Comparisons • "" - (the empty string)
eloquentjavascript.net/03_functions.html
x === true if x is equal to y • null
y • undefined
• NaN - not a number
x !== true if x is different from y
y
x >= y true if x is greater than, or equal to
y
x <= y true if x is less than, or equal to y
By pseud Published 9th March, 2016. Sponsored by CrosswordCheats.com
Last updated 11th March, 2016. Learn to solve cryptic crosswords!
Page 2 of 6. http://crosswordcheats.com