Kawiesh

JS Beginner's Notes

Jul 31st, 2020 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. JavaScript (JS) is one of the most popular and widely used programming languages in the world right now. It's growing faster than any other programming languages. For a long time, it was only used in browsers to build interactive web pages, but these days you can build full blown web or mobile apps as well as real-time networking apps like chats and video streaming services, command-line tools, or even games with JS.
  2.  
  3. JS can run in:
  4. 1.Browser (all browsers have a JS Engine)
  5. 2.Node (C++ program with Chrome's V8 JS Engine)
  6.  
  7. ECMA Script = specification (defines standards)
  8. JS = programming language that confirms to this specification.
  9.  
  10. HTML is all about content,
  11. JS is all about behavior of the content.
  12.  
  13. In order to write JS code in a HTML file, you need a script element. There are two places where you can add a script element:
  14. <head> head section </head>
  15. <body> body section </body>
  16.  
  17. The best practice is to put the script element at the end of the body section after all the existing elements, before </body>
  18. This because of 2 reasons:
  19. 1. The browser parses a HTML file from top to bottom. If you put the script element in the head section, the browser might get busy parsing and executing that JS code rather than rendering the content of the page. This can create a bad user experience.
  20. 2. The code between the script elements almost always needs to talk to the elements on the web page. For example, we may want to show or hide some elements. So by adding the code at the end of the body section you can ensure that all these elements are rendered by the browser.
  21.  
  22. Sometimes you're using 3rd party code that has to be placed in the head section. But these are exceptions.
  23.  
  24.  
  25. Browser>>Inspect Element>>Console
  26. console.log('Hello world');
  27.  
  28. Statement: a piece of code that expresses an action to be carried out.
  29. (In this case, we want to log a message on the console)
  30. All statements in JS should be terminated by a semi colon ;
  31.  
  32. String: a sequence of characters
  33.  
  34. //comment (ignored by the JS engine, not executed)
  35.  
  36.  
  37.  
  38. Separation of concern:
  39. JS code can be very long and look messy. So you don't want to write all the script between the script element. Instead you can store the code in a seperate file, and then reference it using the source (src) attribute.
  40. So instead of <script> blabla </script> <script src=code.js> </script>
  41.  
  42.  
  43.  
  44. ......
  45.  
  46.  
  47.  
  48.  
  49. Variables.
  50. Keyword var or let (preferred)
  51.  
  52. let kdexample= 'Hi';
  53. This is a declaration
  54. kdexample= name/label
  55. case sensitive, cannot containe space or reserved words, cannot start with number
  56. 'Hi'= value
  57.  
  58. This is called camelNotation.
  59.  
  60. Value of variable can change, value of constant can't change. You cannot reassign a constant.
  61.  
  62. const age= 19;
  63.  
  64.  
  65.  
  66. 2 types of type
  67. 1. Primitives/value types
  68. 2. Reference types
  69.  
  70.  
  71. Primitives/Value Types:
  72. String
  73. Numbers
  74. Boolean (true or false)
  75. undefined
  76. null
  77.  
  78.  
  79. JS is a dynamic language. Type of variable can change (based on assigned value)
  80. In console
  81. type of **variable name**
  82. //Outputs the type of variable
  83.  
  84. E.g if value = 'Hi'. Type=string. If value=7 Type=number
  85.  
  86.  
  87. No float or int in JS. All numbers are of type numbers
  88.  
  89.  
  90. Variable types:
  91. String e.g value of 'lolzzz'
  92. Number e.g value of 7
  93. Undefined e.g value of lolzzz
  94. Boolean e.g value of true
  95. Object e.g value of null
  96.  
  97.  
  98. ***Undefined can be a (type of) value but it's also a variable type***
  99.  
  100.  
  101.  
  102. .....
  103.  
  104.  
  105.  
  106.  
  107. let name= 'KD;
  108. let age= 30,
  109.  
  110. Instead of the above notation, we can write this using an object. Makes code look cleaner. The object here is a person. And name & age are properties/keys of this object (person)
  111.  
  112. Object literal syntax example: let x = {key&value pair};
  113.  
  114. let person= {
  115. name: 'KD',
  116. age:30
  117. };
  118.  
  119. How to access properties instead of whole object
  120. 1. Dot notation (Recommended)
  121. person.name
  122. E.g console.log(person.name);
  123. Or to change the property person.name= 'NotKd';
  124.  
  125. 2. Bracket notation (For special cases)
  126. person.['name']
  127. E.g person.['name']= 'NotKd';
  128.  
  129. //Bracket notation useful when you don't know the property name. For example when the property name is determined by user input.
  130. E.g let selection = 'name';
  131. person.[selection] = 'yesKd';
  132. **using the bracket notation to access the property in a dynamic way***
  133.  
  134. console.log(person.name);
  135. Output: yesKD
  136.  
  137.  
  138. Once a variable is declared it can't be re-declared, only redefined.
  139. For example: let age=20;
  140. let age= 40 !!Error!! age has already been declared if you want to change the value you can re-define it e.g age=40 (without let or var)
  141.  
  142.  
  143. Reference types: Array Object Function
Add Comment
Please, Sign In to add comment