JavaScript Basics
JSBIN is an online platform to code and run it, so you can see in real time what you are creating
In JavaScript a string appears like: 'something in a string' but, numbers appear like: 23
Commands
Command Description Example
console.log(…) Comment processed by the
computer, it is an output, so it is It will be seen…
visualized
//… It is a comment that it is not
processed or evaluated by the
computer, so it is not visualized
Single Line Comment
/*…*/ It is a comment just like the
Single Line Comment but you
can just write as many lines as
you want, without having to
do // before every single one of
them (adequate when you want
to write a lot!)
Block Comment
let It is the way for the code to
identify that what follows may
be a variable (see chapter
Variable)
It is the way for a variable to be
declared
const This command is the command
to declare a variable that cannot
change its value along the code
push() Adds to the end of an array, the
content indicated inside the ()
pop()
shift()
unshift()
+ Addiction operator used for:
1. Add 2 numbers
2. Add 2 strings or a
number with a string, to
form a complete
sentence
+= Adds 2 strings, one in front of
the other, using the same
variable
- Subtraction operator
* Multiplication operator
/ Division operator
++ Increments 1
-- Decrements 1
% Remainder of a division between
2 numbers
“<string here> \”<quote Backslash that allows you to
here>\”. <end of string write a quote inside a string
here>”
.length Used to find the length of
something
[] Bracket notation:
Starts from 0
Used to find the letter in the
position inside the brackets
Used to create an array
Used to access the content
inside the indexed number
inside the brackets
== It compares 2 values, without
evaluating if they are strings or
numbers, which means that, for
example 3 = ‘3’
=== It compares 2 values, evaluating
if they are strings or numbers,
which means that, for example 3
≠ ‘3’
!= different
!== Opposite of ==
This means that 2 values are
compared to each other to see if
they are different – evaluating
the type of the values
!=== Opposite of ===
This means that 2 values are
compared to each other to see if
they are different – not
evaluating the type of the values
typeof Identifies the type of the value –
if it’s a string or a number
> Greater than
< Less than
>= Greater or equal than
<= Less or equal than
Function <name of the
function> (<argument
1>,<argument 2>) {}
If (<condition here>)
{<result here>}
&& If and only If
Logical AND
Replaces an if condition inside
another, with only 1 character
placed inside the if condition
like:
If (xxx && yyy) {…}
|| If at least one condition is true
Logical OR
It replaces 2 separated ifs inside
a function, with only 1 character,
like:
If (xxx || yyy) {…}
switch(){
case x:
console.log();
break;
case y:
console.log();
break;
…
Default:
defaultStatement;
break;
}
Variable
A variable is just something that let us store a value
Example:
Let myfavoritenumber = 7
Console.log(myfavoritenumber)
On screen it will appear the number 7
Putting something equal (=) to other thing is called ASSIGNMENT, because you are associating
something to other thing, in this case, you are associating a variable to a value (see chapter
Declaration and Assigment).
Declaration and Assigment
A variable can be declared with the keyword “let”, like this:
Let somefunnything
However, the variable is not being processed, because it is not assigned to a value yet. So, if
we run the code, it will appear “undefined” because it is not associated to nothing.
So, to be processed we have to assigned the variable to a value, like this:
Somefunnything = -54
In this case, the variable means something already, so when we print it, it will appear “-54”.
Possible error: If we make a mistake writing the variables name with the console.log()
command, we get an error like this:
Constants
Variables that cannot be changed are constants.
This means they can only have only 1 value ever, so if you try to give it another variable it will
originate an error, like this: