Guest User

Untitled

a guest
Nov 16th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. # JavaScript Basics
  2.  
  3. JavaScript is a programming language used (mostly) for Web Development. It's the programming language responsible for a large lot of the interactions you perform when you're clicking around (submitting forms, dragging and dropping etc) on a Webpage.
  4.  
  5. All modern browsers can interpret JavaScript and all you need to get started is a text editor (e.g TextEdit, Notepad), but you're advised to get a slightly fancier kind of text editor called an IDE (Integrated Development Environment) like SublimeText, Atom or VS Code.
  6.  
  7. In this chapter, you'll be getting an introduction to writing the latest version of JavaScript.
  8.  
  9. ## Variables
  10.  
  11. A *variable* is where (dynamic) information is stored in a computer program. Say you're signing up for a social network and you're required to submit your email address, the web developer who made the social network needs a way to be able to reference whatever email address you submit in the code. This is done with a variable. The developer creates a variable called `email` to store whatever you submit as your email address.
  12.  
  13. A variable has two important aspects; it's **name** and it's **value**. In the scenario above, the name of the variable is `email` and that is what the developer needs to remember to reference the value stored in that variable. The value might be your email address or someone else's, depending on who just submitted the sign up form.
  14.  
  15. This is how to create a variable in JavaScript.
  16.  
  17. ```
  18. let email = "your@email.com";
  19. ```
  20.  
  21. We used the `let` statement to create a variable called `email` with a value of "your@email.com".
  22.  
  23. To change the value of that variable, all we need to write is:
  24.  
  25. ```
  26. email = "lol@email.com";
  27. ```
  28.  
  29. There are however different kinds of variable values. These are called **Primitive Data Types**. The reason for this separation of types is that, there are certain operations that you can perform on a particular kind of value but not on others. This will become clearer as we go on.
  30.  
  31. Here are some of the **Primitive Data Types** you should know about:
  32.  
  33. ### 1. String
  34.  
  35. A `String` is a type used to represent textual data. The variables in the examples above are all `String`s. You can easily tell if a variable is a `String` if it's surrounded by single (') or double (") quotation marks. With `Strings` you can do text manipulation stuff like prepend it to another `String`, remove all it's vowels, etc
  36.  
  37. ### 2. Number
  38.  
  39. This represents all kinds of numbers. You can create variables of the `Number` type like this.
  40.  
  41. ```
  42. let age = 25;
  43. let width = 12.678;
  44. ```
  45.  
  46. With `Number` type variables, you can perform the kinds of mathematical operations you'd expect. Stuff like:
  47.  
  48. ```
  49. age * width;
  50. age + width;
  51. age - width;
  52. ```
  53.  
  54. You can even assign the value(s) of any of these operations into another `Number` type variable.
  55.  
  56. ```
  57. result = age + width;
  58. ```
  59.  
  60. However, you can't perform these operations on a `String` type variable.
  61.  
  62. ### 3. Boolean
  63.  
  64. `Boolean` types can only be one of two values; `true` or `false`.
  65.  
  66. Finally, there's a special type of variable called a **constant**, and what makes it special is that it's value cannot change once it's set.
  67.  
  68. ```
  69. const age = 39;
  70. ```
  71.  
  72. Above, we created a constant `age` and set it's value to a `Number` 39. This means that throughout the lifetime of that program, `age` will always be 39 and it's value cannot be changed.
  73.  
  74.  
  75. ## Arrays
  76.  
  77. An *Array* is a special variable that allows you to store multiple values in it.
  78.  
  79. Say you have a list of names to store. You could store them in individual variables like this:
  80.  
  81. ```
  82. let name1 = "Timi";
  83. let name2 = "Tolu";
  84. let name3 = "Tade";
  85. ```
  86.  
  87. If you had to count the names, or loop through each one for whatever reason, it'd be really difficult to do that with one variable for each name.
  88.  
  89. An array will let you hold many values under a single variable name.
  90.  
  91. ```
  92. let names = ["Timi", "Tolu", "Tade"];
  93. ```
  94.  
  95. You can access the individual values in the array using the *index*. The *index* of a value in an array is it's position in that array. The quirk with indexes in JavaScript is that we start count from 0, instead of 1.
  96.  
  97. This means that the value `"Timi"` is at `names[0]` and `"Tolu'` is at `names[0]` and so on and so forth.
  98.  
  99. To get the number of *elements* in an array, you use the `length` property as follows
  100.  
  101. ```
  102. names.length;
  103. ```
  104.  
  105. You can also edit values in the array by using the *index* of the position you're trying to change.
  106.  
  107. ```
  108. names[1] = "John";
  109. ```
  110.  
  111. The above code will change the value at that position from `"Tolu"` to `"John"`.
  112.  
  113. You can also combine the `length` property with the ability to edit values at certain positions to add a new item to the end of the array, as follows:
  114.  
  115. ```
  116. names[names.length] = "Ire";
  117. ```
  118.  
  119. The array will now become:
  120.  
  121. ```
  122. ["Timi", "John", "Tade", "Ire"]
  123. ```
  124.  
  125. One neat thing about arrays in JavaScript is that you can have elements of different data types.
  126.  
  127. ```
  128. let randomStuff = [ 1, "Hello", true, false, 1098, "Hey"];
  129. ```
  130.  
  131. The above is a perfectly valid JavaScript array.
  132.  
  133.  
  134. ## Conditionals
  135.  
  136. *Conditionals* help you control what happens if certain criteria or conditions are fulfilled.
  137.  
  138. ### A little bit more about Booleans: Comparison Operators
  139.  
  140. There are certain JavaScript *operators* that allow you to compare two values or two variables. The outcome of those comparisons is always a `Boolean`; either `true` or `false`.
  141.  
  142. | Operator | Description | Example | Outcome |
  143. |----------|--------------|-----------------|---------|
  144. | > | Greater than | 1 > 0 | true |
  145. | < | Less than | 300 < 200 | false |
  146. | === | Equal to | "hey" === "hey" | true |
  147. | != | Not equal to | true != false | true |
  148.  
  149. The above are some of the operators you use in conditionals to determine equality or difference between variables or values.
  150.  
  151. ### If
  152.  
  153. ```
  154. if (age > width){
  155. let result = "Age is greater than width";
  156. }
  157. ```
  158.  
  159. In the above example:
  160.  
  161. - `(age > width)` is the condition we're testing for, this will be `true` because `25` *is* greater than `12.678`.
  162. - The `if` statement tells JavaScript to execute what it is within the curly braces if the condition to be checked is `true`.
  163. - `let result = "Age is greater than width"` is the code within curly braces. This will be run only if `age` is greater than `width`, which it is. As such the value of `result` will now be `"Age is greater than width"`
  164.  
  165. ### else
  166.  
  167. ```
  168. if (1 === 0){
  169. let result = "Everything I know is a lie.";
  170. } else {
  171. let result = "All is well";
  172. }
  173. ```
  174.  
  175. You can add to an `if` statement by using the `else` statement which does runs a block of code when the `if` condition is `false`.
  176.  
  177. Because `1` is NOT equal to `0`, the code in the `else` block will run and the value of `result` will now be `"All is well"`.
  178.  
  179. ### else if
  180.  
  181. An `else if` statement is similar to the `else` statement. But it lets you specify another condition to be tested for. If that condition is `true`, JavaScript will run the code in that block.
  182.  
  183. ```
  184. if (false) {
  185. let result = "if block";
  186. } else if (true) {
  187. let result = "else if block";
  188. } else {
  189. let result = "else block";
  190. }
  191. ```
  192.  
  193. Because the condition preceding the `else if` statement is `true`, the value `result` will be `"else if block"`.
  194.  
  195.  
  196. ## Loops
  197.  
  198. *Loops* provide a way to do something repeatedly (until a certain condition is fulfilled).
  199.  
  200. ### For Loop
  201.  
  202. ```
  203. for (statement 1; statement 2; statement 3) {
  204. code block to be executed
  205. }
  206. ```
  207.  
  208. The above shows the general format of a `for` loop.
  209.  
  210. - **Statement 1** is executed before the loop starts.
  211. - **Statement 2** is a condition. For as long as this condition is `true`, it will continue to run the code block.
  212. - **Statement 3** will run each time after the code block in the loop has been executed.
  213.  
  214.  
  215. ```
  216. let randomArray = []
  217.  
  218. for (i = 0; i < 5; i++) {
  219. randomArray[randomArray.length] = i;
  220. }
  221. ```
  222.  
  223. In the above example, a couple of things are going on:
  224.  
  225. - First of all, we create an empty array for reasons that will become apparent soon.
  226. - Now to the loop, our **Statement 1** sets the value of variable `i` to `0`.
  227. - Our **Statement 2** is the condition. For as long as `i` is less than `5`, JavaScript will keep running the code in between the curly braces.
  228. - **Statement 3** just simply increased the value of `i` and this happens each time the code block in the loop has been executed.
  229. - Finally, our code block; Each time this runs, we put the value of `i` at the time into `randomArray`.
  230.  
  231. As a result, the value of `randomArray` should be.
  232.  
  233. ```
  234. [0, 1, 2, 3, 4]
  235. ```
  236.  
  237.  
  238. ### While Loop
  239.  
  240. This is similar to the `for` loop, but a bit simpler to construct.
  241.  
  242. ```
  243. while (i < 10) {
  244. randomArray[randomArray.length] = i;
  245. i++;
  246. }
  247. ```
  248.  
  249. The above simply instructs JavaScript to keep running the code between the curly braces as long as the variable `i` is less than `10`.
Add Comment
Please, Sign In to add comment