Introduction to JavaScript (1)

Pav_Ka
PavKa
Published in
3 min readMar 14, 2017

Basics, Loops, Switch statement

Taking notes while learning is something we are all used to practise in one way or another, whether it’s a notebook and pencil or a laptop. It helps us emphasise and organise information. It helps us memorise and keeps our minds actively involved in what we hear or read while studying.

I’ve decided to keep a little blog while I am learning to create a condensed record for study. I do believe that a set of concise, well-organised notes from each session will help me learn and review.

I appreciate this might not be exhaustively accurate at this stage but I intend to update it regularly and correct or improve any information provided.

This blog is mostly based on ‘Eloquent JavaScript’ by Marijn Haverbeke.

The Basics

There are six types of values in JavaScrpt:

  • Numbers
  • Strings
  • Boolean
  • Objects
  • Functions
  • Undefined

Arithmetic operators:

+   addition
- subtraction
* multiplication
/ division
% modulus
++ increment
-- decrement

Assignment operators:

=   assignment
+= addition assignment
-= subtraction assignment
*= multiplication assignment
/= division assignment
%= reminder assignment

Comparison operators:

==   equal
!= not equal
=== strict equal
!== strict not equal
> greater than
>= greater than or equal
< less than
<= less than or equal

Logical operators:

&&   logical AND
|| logical OR
! logical NOT

Loops

The examples below do exactly the same task but in a slightly different way…

While loop:

var i = 0;
while (i <= 20) {
console.log('This is number '+i+'');
i = i + 2;
}
//This example will only run if the condition is true...//use when you don't know how many times you want the loop to run...

Do while loop:

var i = 21;
do {
console.log('This is number '+i+'');
i = i + 2;
}while(i <= 20);
//This loop will ensure you run the code at least once before checking the condition...//use when you don't know how many times you want the loop to run but you know it must run at least once...

For loop:

for (i = 0; i <= 20; i = i + 2) {
console.log('This is number '+i+'');
}
//This loop starts with three statements:
//defining a variable (like a counter)
//expression that checks or condition
//updating the loop after every iteration or increment...
//This way you can avoid a mistake by forgetting to add an increment //at the end of 'while loop'...
//use if you know how many time you want the loop run...

Breaking out of a loop:

Having the loop’s condition produce false is not the only way a loop can finish. There is a special statement called break that has the effect of immediately jumping out of the enclosing loop.

for (var current = 20; ; current++) {
if (current % 7 == 0)
break;
}

Switch statement

Use the switch statement to select one of many blocks of code to be executed.

switch (prompt("What is the weather like?")) {
case "rainy":
console.log("Remember to bring an umbrella.");
break;
case "sunny":
console.log("Dress lightly.");
case "cloudy":
console.log("Go outside.");
break;
default:
console.log("Unknown weather type!");
break;
}

You may put any number of case labels inside the block opened by switch. The program will jump to the label that corresponds to the value that switchwas given or to default if no matching value is found. It starts executing statements there, even if they’re under another label, until it reaches a breakstatement. In some cases, such as the "sunny" case in the example, this can be used to share some code between cases (it recommends going outside for both sunny and cloudy weather). But beware: it is easy to forget such a break, which will cause the program to execute code you do not want executed.

To see some useful tasks with loops and learn about truthy/falsy visit part 2.

One last thing…

If you liked this article, click the💚 below so other people will see it here on Medium.

--

--