{"flag":true,"single":true,"pageTitle":"Loops & if else conditions in javascript","post":{"id":206,"user_id":"1","slug":"loops-if-else-conditions-in-javascript-ne9u","title":"Loops & if else conditions in javascript","body":"<p><strong>1.if<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>if (condition) {\r\n  \/\/  block of code to be executed if the condition is true\r\n}<\/code><\/pre>\r\n<p><strong>2.if else<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>if (condition) {\r\n  \/\/  block of code to be executed if the condition is true\r\n} else {\r\n  \/\/  block of code to be executed if the condition is false\r\n}<\/code><\/pre>\r\n<p><strong>3.if else if<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>if (condition1) {\r\n  \/\/  block of code to be executed if condition1 is true\r\n} else if (condition2) {\r\n  \/\/  block of code to be executed if the condition1 is false and condition2 is true\r\n} else {\r\n  \/\/  block of code to be executed if the condition1 is false and condition2 is false\r\n}<\/code><\/pre>\r\n<p><strong>4.switch<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>switch(expression) {\r\n  case x:\r\n    \/\/ code block\r\n    break;\r\n  case y:\r\n    \/\/ code block\r\n    break;\r\n  default:\r\n    \/\/ code block\r\n}\r\n<\/code><\/pre>\r\n<p><strong>5.For Loop<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>for (i = 0; i &lt; 5; i++) {\r\n  text += \"The number is \" + i + \"&lt;br&gt;\";\r\n}<\/code><\/pre>\r\n<p><strong>6.FOR IN (properties of object read)<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>var person = {fname:\"John\", lname:\"Doe\", age:25};\r\nvar text = \"\";\r\nvar x;\r\nfor (x in person) {\r\n  text += person[x];\r\n} \r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ OR\r\n\r\nfor (var key in person) {\r\n    if (p.hasOwnProperty(key)) {\r\n        console.log(key + \" -&gt; \" + p[key]);\r\n    }\r\n}<\/code><\/pre>\r\n<p><strong>7.FOR OF (iterable for objects ,arrays, nodes,strings etc)<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>var cars = ['BMW', 'Volvo', 'Mini'];\r\nvar x;\r\nfor (x of cars) {\r\n  document.write(x + \" \"); \/\/BMW Volvo Mini\r\n}<\/code><\/pre>\r\n<p><strong>8.while<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>while (i &lt; 10) {\r\n  text += \"The number is \" + i;\r\n  i++;\r\n}<\/code><\/pre>\r\n<p><strong>9.do while&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>do {\r\n  text += \"The number is \" + i;\r\n  i++;\r\n}\r\nwhile (i &lt; 10);\r\n<\/code><\/pre>\r\n<p><strong>10.break; \/\/break statement used to jump out of a loop.&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>for (i = 0; i &lt; 10; i++) {\r\n  if (i === 3) { break; }\r\n  text += \"The number is \" + i + \"&lt;br&gt;\";\r\n}<\/code><\/pre>\r\n<p><strong>11.continue;\/\/ jump out one iteration<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>for (i = 0; i &lt; 10; i++) {\r\n  if (i === 3) { continue; }\r\n  text += \"The number is \" + i + \"&lt;br&gt;\";\r\n}<\/code><\/pre>\r\n<p>12. Loop objects with keys<\/p>\r\n<pre class=\"language-markup\"><code> let future_all_coins = {'usd':1,'btc','38000','ltc':108};\r\n  var future_all_coins_keys = Object.keys( future_all_coins);\r\n\r\n  for(let coin of future_all_coins_keys){\r\n    console.log(coin+\" \"+future_all_coins[coin]);\r\n  }<\/code><\/pre>","category_id":"12","is_private":"0","created_at":"2023-10-13T01:34:22.000000Z","updated_at":"2023-10-13T01:34:22.000000Z","category":{"id":12,"user_id":"1","name":"Javascript","slug":"javascript-xplw","parent_id":null,"created_at":"2023-03-23T02:24:43.000000Z","updated_at":"2023-03-23T02:24:43.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"1.if if (condition) {   \/\/  block of code to be executed if the condition is true } 2.if else if (condition) {   \/\/  block of code to be exe - Loops & if else conditions in javascript (Updated: October 13, 2023) - Read more about Loops & if else conditions in javascript at my programming site [SITE]","categories":[]}