Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. # JS Basics to Intermediate JS
  2.  
  3. ## 1. Conditional Operators
  4.  
  5. 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.
  6.  
  7. ```
  8. | CONDITIONAL OPERATOR | TRANSLATION | COMPARING | RESULT |
  9. |----------------------|-------------|---------------------|--------|
  10. | && | and | (1 < 2) && (4 > 2) | true |
  11. | || | or | (1 < 2) || (4 < 2) | true |
  12. | ! | not | (5 + 5) != (10 + 2) | true |
  13. | == | equals | 5 == '5' | true |
  14. | === | equals | 5 === '5' | false |
  15. ```
  16.  
  17. ## 2. Control Flow: if-else
  18.  
  19. 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.
  20.  
  21. ``` JAVASCRIPT
  22. // So imagine you have a piece of software to simplify the process of letting people into a club based on their age.
  23.  
  24. let age = window.prompt('Enter the persons age:');
  25.  
  26. if(age >= 18){ //If the person is over 18, grant them entry
  27. alert('Let the person in the club!')
  28. } else if (age >= 16){ //If they are over 16 but under 18, no entry!
  29. alert('Sorry No Entry Allowed!')
  30. } else { // If they fall outside of that. Call the parents.
  31. alert("Call the parents, they shouldn't even be out!")
  32. }
  33.  
  34.  
  35. ```
  36.  
  37. ## 3. Control Flow: loops
  38.  
  39. 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!!!
  40.  
  41. ``` JAVASCRIPT
  42. // While loop
  43.  
  44. let status = true;
  45. let counter = 0;
  46.  
  47. while (status == true){
  48.  
  49. if(counter == 20){
  50. console.log('Loop Ended!')
  51. counter += 1
  52. status = false
  53. } else {
  54. counter += 1
  55. console.log(counter)
  56. }
  57.  
  58. }
  59.  
  60. // infinite loop
  61.  
  62. while (status == true){
  63. console.log('HELP ME IM STUCK IN AN INFINITE LOOP!')
  64. }
  65.  
  66. // for loop
  67.  
  68. let array = ['John', 'Alice', 'Steven', 'Nicholas', 'Emika', 'Edwin', 'Liren']
  69.  
  70. for(let i = 0; i < array.length; i++){
  71. console.log(`Name @ position ${i + 1} is ${array[i]}`)
  72. }
  73.  
  74. // while loop with break
  75.  
  76. let counter = 0;
  77.  
  78. while (counter < array.length){
  79.  
  80. if(array[counter] == 'Steven'){
  81. alert('I found Steven')
  82. break;
  83. }
  84.  
  85. console.log(array[counter])
  86. counter++
  87.  
  88. }
  89.  
  90.  
  91.  
  92. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement