Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # JS Basics to Intermediate JS
- ## 1. Conditional Operators
- This is an important basic feature of not just JavaScript but any programming language. Although keep in mind that not all languages follow the same syntax. They are a very powerful tool when you are building logic and also when dealing with control flow as they will form the basis of your conditional statements.
- ```
- | CONDITIONAL OPERATOR | TRANSLATION | COMPARING | RESULT |
- |----------------------|-------------|---------------------|--------|
- | && | and | (1 < 2) && (4 > 2) | true |
- | || | or | (1 < 2) || (4 < 2) | true |
- | ! | not | (5 + 5) != (10 + 2) | true |
- | == | equals | 5 == '5' | true |
- | === | equals | 5 === '5' | false |
- ```
- ## 2. Control Flow: if-else
- Sometimes when we write code, we want specific portions of code to run under different conditions, this is where control flow with if-else statements becomes a very useful tool. See the example below for a good simple case use of an if-else statement.
- ``` JAVASCRIPT
- // So imagine you have a piece of software to simplify the process of letting people into a club based on their age.
- let age = window.prompt('Enter the persons age:');
- if(age >= 18){ //If the person is over 18, grant them entry
- alert('Let the person in the club!')
- } else if (age >= 16){ //If they are over 16 but under 18, no entry!
- alert('Sorry No Entry Allowed!')
- } else { // If they fall outside of that. Call the parents.
- alert("Call the parents, they shouldn't even be out!")
- }
- ```
- ## 3. Control Flow: loops
- When you write code, it would be pretty dumb if you had to write out multiple lines of code that is repeated over and over again to perform actions on the same piece of data. Thankfully there is very useful tool built into most languages that makes performing these repeated tasks super simple. What I am talking about is loops. There are a few different ways in which you can use loops and there are also some built into JavaScript that I haven't covered here but I will leave it to you to discover those for yourself. BECAREFUL WHEN YOU ARE USING LOOPS. IF THE LOOP ENDING CONDITION ISN'T SPECIFIED OR DOESN'T REACH YOU WILL GET CAUGHT IN SOMETHING CALLED AN INFINITE LOOP!!!
- ``` JAVASCRIPT
- // While loop
- let status = true;
- let counter = 0;
- while (status == true){
- if(counter == 20){
- console.log('Loop Ended!')
- counter += 1
- status = false
- } else {
- counter += 1
- console.log(counter)
- }
- }
- // infinite loop
- while (status == true){
- console.log('HELP ME IM STUCK IN AN INFINITE LOOP!')
- }
- // for loop
- let array = ['John', 'Alice', 'Steven', 'Nicholas', 'Emika', 'Edwin', 'Liren']
- for(let i = 0; i < array.length; i++){
- console.log(`Name @ position ${i + 1} is ${array[i]}`)
- }
- // while loop with break
- let counter = 0;
- while (counter < array.length){
- if(array[counter] == 'Steven'){
- alert('I found Steven')
- break;
- }
- console.log(array[counter])
- counter++
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement