Guest User

Untitled

a guest
Dec 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9. <button>Test</button>
  10. <script id="jsbin-javascript">
  11. console.log('Let us learn some ES6')
  12.  
  13. var fName = 'ankit'
  14. let lName = 'nigam'
  15.  
  16. if (true){
  17. let newlName = 'sharma'
  18. var newfName = 'Vikash'
  19. }
  20.  
  21. console.log(fName)
  22. console.log(lName)
  23. console.log(newfName) // var variable declared inside block is acceible from block
  24. //console.log(newlName) //let variable declared is not accesible from block
  25.  
  26. // let and var are used as normal variables instead let are not allowed to access from putside of declaring scope like above.
  27.  
  28.  
  29. // const holds the value but not allowed to chnage the value within.
  30. const arrayRol = [1,2,3,4]
  31. console.log(arrayRol)
  32. arrayRol.push(6) // here we are not changing the mamory address of array but only pushing the new value in it.
  33. console.log(arrayRol)
  34.  
  35. // here new object is created.
  36. const obj = {
  37. age : 27
  38. }
  39.  
  40. // if we try to change the value in obj it throws the error.
  41. // obj = {}
  42.  
  43. console.log(obj.age)
  44. // here we are only chnage the value inside the const not the value of const.
  45. obj.age = 28
  46.  
  47. console.log(obj.age)
  48.  
  49.  
  50. //-------------- function --------------
  51.  
  52. function printHello()
  53. {
  54. console.log('hello')
  55. }
  56.  
  57. printHello()
  58.  
  59. var fHello = () => {console.log('hello fat arrow function')}
  60.  
  61. fHello()
  62.  
  63. // Return from Arrow function
  64. var addNumber = () => {
  65. let a = 1
  66. let b = 2
  67.  
  68. return a+b
  69. }
  70.  
  71. console.log(addNumber())
  72.  
  73. // Param in arrow function
  74. var addNumberWithParams = (a,b) => {
  75.  
  76.  
  77. return a+b
  78. }
  79.  
  80. console.log(addNumberWithParams(3,4))
  81.  
  82. // Single param mat not need parantheses in arrow function.
  83. var addNumberWithParamNoParanthese = a => a + a
  84.  
  85. console.log(addNumberWithParamNoParanthese(2))
  86.  
  87.  
  88. // this object
  89.  
  90. function funcThis()
  91. {
  92. console.log(this)
  93. }
  94.  
  95. var funcThis2 = () => console.log(this)
  96.  
  97. funcThis()
  98.  
  99. var button = document.querySelector('button')
  100. button.addEventListener('click',funcThis2)
  101.  
  102. // here this refers to calling button when used with normal function , but when used with arrrow function this refers to its function context.
  103.  
  104. //------------Default-----------
  105.  
  106. function checkDefault(a = 10 , b){
  107. return a + b
  108. }
  109.  
  110. console.log(checkDefault())
  111.  
  112.  
  113. //------------Object ------------
  114.  
  115. let name = 'ankit'
  116. let age = 30
  117.  
  118. const objUser = {
  119. name,
  120. age
  121. }
  122. // here object take value from its sorrunding.
  123. console.log(objUser)
  124.  
  125. //----------- Dynamic Properties ---------
  126.  
  127. let dynamicAge ='age'
  128.  
  129. let dynamicProperties = {
  130. "name":'max',
  131. [dynamicAge] : 20
  132. }
  133.  
  134. console.log(dynamicProperties["name"],dynamicProperties[dynamicAge])
  135.  
  136.  
  137. //-------------- rest operator
  138.  
  139. function addNumbers(...numbers){
  140. let result = 0
  141. for(let i = 0; i<numbers.length ; i++){
  142. result += numbers[i]
  143. }
  144.  
  145. return result
  146. }
  147.  
  148. console.log(addNumbers(10,20,30))
  149.  
  150. //---------------- spread operator
  151.  
  152. let array1 = [1,2,3,4]
  153. let array2 = [6,7,8,9]
  154.  
  155. let combinedArray = [...array1,...array2]
  156.  
  157. console.log(combinedArray)
  158.  
  159.  
  160. //---------------- template expression - multiline string -------
  161.  
  162. let multilineString = `my name is
  163. ${name}` // here name var is used as expression
  164.  
  165. console.log(multilineString)
  166.  
  167. //------------ for of loop--------
  168.  
  169. function printAll(){
  170. for(item of combinedArray){
  171. console.log(item)
  172. }
  173. }
  174.  
  175. printAll()
  176.  
  177. //--------- destructuring ---------
  178.  
  179. let [a,b] = array1
  180.  
  181. console.log("Destructured \n " + a +' '+ b)
  182.  
  183. let {names,ages} = objUser // destructing by name of variable , it returned undefined bcoz 'names' is not declared in ObjUser
  184. console.log(names)
  185.  
  186. let newObj = {
  187. myName:'ankit',
  188. myNumber : 844934242342
  189. }
  190.  
  191. let {myName, myNumber : myNumberAlias} = newObj
  192.  
  193. //console.log(myName + " " + myNumber)
  194. console.log(myNumberAlias)
  195. // After giving alias to a destructured var it old name will give error.
  196. </script>
  197.  
  198.  
  199.  
  200. <script id="jsbin-source-javascript" type="text/javascript">console.log('Let us learn some ES6')
  201.  
  202. var fName = 'ankit'
  203. let lName = 'nigam'
  204.  
  205. if (true){
  206. let newlName = 'sharma'
  207. var newfName = 'Vikash'
  208. }
  209.  
  210. console.log(fName)
  211. console.log(lName)
  212. console.log(newfName) // var variable declared inside block is acceible from block
  213. //console.log(newlName) //let variable declared is not accesible from block
  214.  
  215. // let and var are used as normal variables instead let are not allowed to access from putside of declaring scope like above.
  216.  
  217.  
  218. // const holds the value but not allowed to chnage the value within.
  219. const arrayRol = [1,2,3,4]
  220. console.log(arrayRol)
  221. arrayRol.push(6) // here we are not changing the mamory address of array but only pushing the new value in it.
  222. console.log(arrayRol)
  223.  
  224. // here new object is created.
  225. const obj = {
  226. age : 27
  227. }
  228.  
  229. // if we try to change the value in obj it throws the error.
  230. // obj = {}
  231.  
  232. console.log(obj.age)
  233. // here we are only chnage the value inside the const not the value of const.
  234. obj.age = 28
  235.  
  236. console.log(obj.age)
  237.  
  238.  
  239. //-------------- function --------------
  240.  
  241. function printHello()
  242. {
  243. console.log('hello')
  244. }
  245.  
  246. printHello()
  247.  
  248. var fHello = () => {console.log('hello fat arrow function')}
  249.  
  250. fHello()
  251.  
  252. // Return from Arrow function
  253. var addNumber = () => {
  254. let a = 1
  255. let b = 2
  256.  
  257. return a+b
  258. }
  259.  
  260. console.log(addNumber())
  261.  
  262. // Param in arrow function
  263. var addNumberWithParams = (a,b) => {
  264.  
  265.  
  266. return a+b
  267. }
  268.  
  269. console.log(addNumberWithParams(3,4))
  270.  
  271. // Single param mat not need parantheses in arrow function.
  272. var addNumberWithParamNoParanthese = a => a + a
  273.  
  274. console.log(addNumberWithParamNoParanthese(2))
  275.  
  276.  
  277. // this object
  278.  
  279. function funcThis()
  280. {
  281. console.log(this)
  282. }
  283.  
  284. var funcThis2 = () => console.log(this)
  285.  
  286. funcThis()
  287.  
  288. var button = document.querySelector('button')
  289. button.addEventListener('click',funcThis2)
  290.  
  291. // here this refers to calling button when used with normal function , but when used with arrrow function this refers to its function context.
  292.  
  293. //------------Default-----------
  294.  
  295. function checkDefault(a = 10 , b){
  296. return a + b
  297. }
  298.  
  299. console.log(checkDefault())
  300.  
  301.  
  302. //------------Object ------------
  303.  
  304. let name = 'ankit'
  305. let age = 30
  306.  
  307. const objUser = {
  308. name,
  309. age
  310. }
  311. // here object take value from its sorrunding.
  312. console.log(objUser)
  313.  
  314. //----------- Dynamic Properties ---------
  315.  
  316. let dynamicAge ='age'
  317.  
  318. let dynamicProperties = {
  319. "name":'max',
  320. [dynamicAge] : 20
  321. }
  322.  
  323. console.log(dynamicProperties["name"],dynamicProperties[dynamicAge])
  324.  
  325.  
  326. //-------------- rest operator
  327.  
  328. function addNumbers(...numbers){
  329. let result = 0
  330. for(let i = 0; i<numbers.length ; i++){
  331. result += numbers[i]
  332. }
  333.  
  334. return result
  335. }
  336.  
  337. console.log(addNumbers(10,20,30))
  338.  
  339. //---------------- spread operator
  340.  
  341. let array1 = [1,2,3,4]
  342. let array2 = [6,7,8,9]
  343.  
  344. let combinedArray = [...array1,...array2]
  345.  
  346. console.log(combinedArray)
  347.  
  348.  
  349. //---------------- template expression - multiline string -------
  350.  
  351. let multilineString = `my name is
  352. ${name}` // here name var is used as expression
  353.  
  354. console.log(multilineString)
  355.  
  356. //------------ for of loop--------
  357.  
  358. function printAll(){
  359. for(item of combinedArray){
  360. console.log(item)
  361. }
  362. }
  363.  
  364. printAll()
  365.  
  366. //--------- destructuring ---------
  367.  
  368. let [a,b] = array1
  369.  
  370. console.log("Destructured \n " + a +' '+ b)
  371.  
  372. let {names,ages} = objUser // destructing by name of variable , it returned undefined bcoz 'names' is not declared in ObjUser
  373. console.log(names)
  374.  
  375. let newObj = {
  376. myName:'ankit',
  377. myNumber : 844934242342
  378. }
  379.  
  380. let {myName, myNumber : myNumberAlias} = newObj
  381.  
  382. //console.log(myName + " " + myNumber)
  383. console.log(myNumberAlias)
  384. // After giving alias to a destructured var it old name will give error.
  385.  
  386. </script></body>
  387. </html>
Add Comment
Please, Sign In to add comment