Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="iii" content="Operators">
  6. <title>Operators</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /*
  12. * iii. OPERATORS
  13. *
  14. * JavaScript operators allow us to perform arithmetic, logical, and
  15. * various other operations on one or more values.
  16. */
  17.  
  18. /*
  19. * a. Assignment
  20. */
  21.  
  22. // The assignment operator (=) assigns a value to a variable.
  23.  
  24. "use strict";
  25.  
  26. var x = 12;
  27. console.log(x); //prints => 12
  28.  
  29. // Shorthand variations of this operator include +=, -=, *=, /=, and %=.
  30.  
  31. x += y; // same as x = x + y
  32. x -= y; // same as x = x - y
  33. x *= y; // same as x = x * y
  34. x /= y; // same as x = x / y
  35. x %= y; // same as x = x % y
  36.  
  37. /*
  38. * b. Arithmetic
  39. */
  40.  
  41. /*
  42. * Arithmetic operators are used to perform arithmetic using numbers
  43. * or the variables that store numbers:
  44. */
  45. var x = 10,
  46. y = 2;
  47.  
  48. console.log(x + y); //prints 12
  49. console.log(x - y); //prints 8
  50. console.log(x * y); //prints 20
  51. console.log(x / y); //prints 5
  52. console.log(x % y); //prints 0
  53.  
  54. /*
  55. *NOTE: Modulo (%), or the remainder operator, is used to show the
  56. * remainder from dividing two numbers:
  57. */
  58.  
  59. console.log(10 % 3); //prints 1
  60.  
  61. /*
  62. * There is also an increment (++) and a decrement (--) operator, which
  63. * increase or decrease a value by 1:
  64. */
  65.  
  66. console.log(++x); //prints 11
  67. console.log(--y); //prints 1
  68.  
  69. /*
  70. * Notice that x++ is the same as x = x + 1, or x += 1.
  71. * x-- is the same as x = x - 1, or x -= 1.
  72. */
  73.  
  74. // The addition operator (+) can also be used to join, or concatenate, strings:
  75.  
  76. var greeting = "Hello, my name is ";
  77. var myName = "Connor";
  78.  
  79. console.log(greeting + myName); //prints "Hello, my name is Connor"
  80.  
  81. /*
  82. * c. Comparison
  83. */
  84.  
  85. /*
  86. * Comparison operators are used to test for true or false. These operators
  87. * include the greater than(>) and less than(<) operators,
  88. * the equals operators(==, ===), and the bang operator(!).
  89. */
  90.  
  91. // Greater than and Less than:
  92. var x = 10,
  93. y = 2;
  94. console.log(x > 2); //prints true
  95. console.log(x < 2); //prints false
  96.  
  97. // Greater than or equal to(>=) and Less than or equal to(<=):
  98. var x = 10,
  99. y = 10;
  100. console.log(x >= y); //prints true
  101. console.log(x <= y); //prints true
  102.  
  103. /*
  104. * a triple equals sign (===) returns true if two variables or
  105. * values are the same value AND type:
  106. */
  107. var x = 10,
  108. y = 10;
  109. console.log(x === y); //prints true
  110.  
  111. y = 5;
  112. console.log(x === y); //prints false
  113.  
  114. y = "ten";
  115. console.log(x === y); //prints false
  116.  
  117. /*
  118. * a double equals sign (==) returns true if two variables or values are
  119. * the same value. JavaScript will try to convert types to match other types,
  120. * but it can be finicky:
  121. */
  122.  
  123. console.log(10 == "10"); //prints true
  124. console.log(10 == "ten"); //prints false
  125.  
  126. /*
  127. * The bang (!) operator is used to negate the equals sign:
  128. */
  129.  
  130. console.log(5 !== 5); //prints false
  131. console.log(5 !== 8); //prints true
  132.  
  133. /*
  134. * d. Logical
  135. */
  136.  
  137. /*
  138. * Logical operators determine the logical relationships between two
  139. * or more variables or values. These include and(&&), or(||), and not(!).
  140. */
  141.  
  142. // With and(&&), both conditions must be true for the Boolean value to be true:
  143.  
  144. console.log(3 < 10 && 12 > 9); //prints true
  145. console.log(3 !== 5 && 2 < 1); //prints false
  146.  
  147. // With or(||), either condition can be true:
  148.  
  149. console.log(3 > 4 || 1 === 1); //prints true
  150. console.log(1 === 1 || 1 !== 1); // prints true
  151.  
  152. // Not(!) simply negates a statement:
  153.  
  154. console.log(!5); //prints false
  155. console.log(!(3 < 1)); //prints true
  156.  
  157. /*
  158. * e. Binary
  159. */
  160.  
  161. /*
  162. * Binary operators are simply the operators that require two values:
  163. * one on each side of the operator. These include all of the assignment,
  164. * arithmetic, and logical operators that we have covered so far, in
  165. * addition to the instanceof operator, which is outlined below.
  166. */
  167.  
  168. // instanceof lets us know if a given value is an instance of a certain type:
  169. var theGang = ["Dennis", "Dee", "Charlie", "Mac", "Frank"];
  170.  
  171. console.log(theGang instanceof Array); //prints true
  172. console.log(theGang instanceof String); //prints false
  173. console.log(theGang instanceof Number); //prints false
  174.  
  175. /*
  176. * f. Ternary
  177. */
  178.  
  179. /*
  180. * There is one ternary operator in JavaScript, sometimes referred to as
  181. * the conditional operator. It gives a condition, and two expressions;
  182. * if the condition is true, it returns the value of the first expression,
  183. * if the condition is false, it returns the value of the second. Below
  184. * is the syntax and examples:
  185. */
  186.  
  187. // condition ? expression1 : expression2
  188.  
  189. var x = 5,
  190. y = 10;
  191.  
  192. console.log(10 >= 8 ? x + y : x - y); //prints 15
  193. console.log(x >= y ? x + y : x - y); //prints -5
  194. </script>
  195.  
  196.  
  197.  
  198. <script id="jsbin-source-javascript" type="text/javascript">/*
  199. * iii. OPERATORS
  200. *
  201. * JavaScript operators allow us to perform arithmetic, logical, and
  202. * various other operations on one or more values.
  203. */
  204.  
  205. /*
  206. * a. Assignment
  207. */
  208.  
  209. // The assignment operator (=) assigns a value to a variable.
  210.  
  211. var x = 12;
  212. console.log(x); //prints => 12
  213.  
  214. // Shorthand variations of this operator include +=, -=, *=, /=, and %=.
  215.  
  216. x += y; // same as x = x + y
  217. x -= y; // same as x = x - y
  218. x *= y; // same as x = x * y
  219. x /= y; // same as x = x / y
  220. x %= y; // same as x = x % y
  221.  
  222. /*
  223. * b. Arithmetic
  224. */
  225.  
  226. /*
  227. * Arithmetic operators are used to perform arithmetic using numbers
  228. * or the variables that store numbers:
  229. */
  230. var x = 10, y = 2;
  231.  
  232. console.log(x + y); //prints 12
  233. console.log(x - y); //prints 8
  234. console.log(x * y); //prints 20
  235. console.log(x / y); //prints 5
  236. console.log(x % y); //prints 0
  237.  
  238. /*
  239. *NOTE: Modulo (%), or the remainder operator, is used to show the
  240. * remainder from dividing two numbers:
  241. */
  242.  
  243. console.log(10 % 3); //prints 1
  244.  
  245. /*
  246. * There is also an increment (++) and a decrement (--) operator, which
  247. * increase or decrease a value by 1:
  248. */
  249.  
  250. console.log(++x); //prints 11
  251. console.log(--y); //prints 1
  252.  
  253. /*
  254. * Notice that x++ is the same as x = x + 1, or x += 1.
  255. * x-- is the same as x = x - 1, or x -= 1.
  256. */
  257.  
  258. // The addition operator (+) can also be used to join, or concatenate, strings:
  259.  
  260. var greeting = "Hello, my name is ";
  261. var myName = "Connor";
  262.  
  263. console.log(greeting + myName); //prints "Hello, my name is Connor"
  264.  
  265. /*
  266. * c. Comparison
  267. */
  268.  
  269. /*
  270. * Comparison operators are used to test for true or false. These operators
  271. * include the greater than(>) and less than(<) operators,
  272. * the equals operators(==, ===), and the bang operator(!).
  273. */
  274.  
  275. // Greater than and Less than:
  276. var x = 10, y = 2;
  277. console.log(x > 2); //prints true
  278. console.log(x < 2); //prints false
  279.  
  280. // Greater than or equal to(>=) and Less than or equal to(<=):
  281. var x = 10, y = 10;
  282. console.log(x >= y); //prints true
  283. console.log(x <= y); //prints true
  284.  
  285. /*
  286. * a triple equals sign (===) returns true if two variables or
  287. * values are the same value AND type:
  288. */
  289. var x = 10, y = 10;
  290. console.log(x === y); //prints true
  291.  
  292. y = 5;
  293. console.log(x === y); //prints false
  294.  
  295. y = "ten";
  296. console.log(x === y); //prints false
  297.  
  298. /*
  299. * a double equals sign (==) returns true if two variables or values are
  300. * the same value. JavaScript will try to convert types to match other types,
  301. * but it can be finicky:
  302. */
  303.  
  304. console.log(10 == "10"); //prints true
  305. console.log(10 == "ten"); //prints false
  306.  
  307. /*
  308. * The bang (!) operator is used to negate the equals sign:
  309. */
  310.  
  311. console.log(5 !== 5); //prints false
  312. console.log(5 !== 8); //prints true
  313.  
  314. /*
  315. * d. Logical
  316. */
  317.  
  318. /*
  319. * Logical operators determine the logical relationships between two
  320. * or more variables or values. These include and(&&), or(||), and not(!).
  321. */
  322.  
  323. // With and(&&), both conditions must be true for the Boolean value to be true:
  324.  
  325. console.log(3 < 10 && 12 > 9); //prints true
  326. console.log(3 !== 5 && 2 < 1); //prints false
  327.  
  328. // With or(||), either condition can be true:
  329.  
  330. console.log(3 > 4 || 1 === 1); //prints true
  331. console.log(1 === 1 || 1 !== 1); // prints true
  332.  
  333. // Not(!) simply negates a statement:
  334.  
  335. console.log(!5); //prints false
  336. console.log(!(3 < 1)); //prints true
  337.  
  338. /*
  339. * e. Binary
  340. */
  341.  
  342. /*
  343. * Binary operators are simply the operators that require two values:
  344. * one on each side of the operator. These include all of the assignment,
  345. * arithmetic, and logical operators that we have covered so far, in
  346. * addition to the instanceof operator, which is outlined below.
  347. */
  348.  
  349. // instanceof lets us know if a given value is an instance of a certain type:
  350. var theGang = ["Dennis", "Dee", "Charlie", "Mac", "Frank"];
  351.  
  352. console.log(theGang instanceof Array); //prints true
  353. console.log(theGang instanceof String); //prints false
  354. console.log(theGang instanceof Number); //prints false
  355.  
  356.  
  357. /*
  358. * f. Ternary
  359. */
  360.  
  361. /*
  362. * There is one ternary operator in JavaScript, sometimes referred to as
  363. * the conditional operator. It gives a condition, and two expressions;
  364. * if the condition is true, it returns the value of the first expression,
  365. * if the condition is false, it returns the value of the second. Below
  366. * is the syntax and examples:
  367. */
  368.  
  369. // condition ? expression1 : expression2
  370.  
  371. var x = 5, y = 10;
  372.  
  373. console.log(10 >= 8 ? x + y : x - y); //prints 15
  374. console.log(x >= y ? x + y : x - y); //prints -5
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396. </script></body>
  397. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement