Skip to content

Commit aecafc4

Browse files
committed
added loops, conditions, and prompts
1 parent 51a16a3 commit aecafc4

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

README.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,165 @@ console.log(slice); // prints [3, 4, 5, 6, 7] to the console
153153
console.log(arr); // prints [1, 2, 8, 9]
154154
```
155155

156+
## Conditions
157+
158+
Conditions are used to check whether a condition is true or false, and based on that we can perform some operations.
159+
160+
**If-Else**
161+
```javascript
162+
const a = 10;
163+
164+
if (a == 10) {
165+
console.log("Value is 10");
166+
} else {
167+
console.log("Value is not 10");
168+
}
169+
```
170+
171+
The general syntax for the if-else condition looks like above. The else part is optional, if you don't want to use it, then you can just use the if part.
172+
173+
Compared to other programming languages, JS has a lot of operators, which can be used to check the conditions.
174+
175+
**Comparison Operators**
176+
```javascript
177+
const a = 10;
178+
const b = "10";
179+
180+
console.log(a == b); // prints true to the console
181+
console.log(a === b); // prints false to the console
182+
```
183+
184+
The difference between `==` and `===` is that, `==` checks only the value of the variable, whereas `===` checks the value and the datatype of the variable.
185+
186+
The negation of the above is `!=` and `!==`.
187+
188+
**Inequality Operators**
189+
```javascript
190+
const a = 10;
191+
const b = 20;
192+
193+
console.log(a > b); // prints false to the console
194+
console.log(a < b); // prints true to the console
195+
console.log(a >= b); // prints false to the console
196+
console.log(a <= b); // prints true to the console
197+
```
198+
199+
The above operators are used to check the relation between two variables.
200+
201+
**Logical Operators**
202+
```javascript
203+
const a = 10;
204+
const b = 20;
205+
const c = 30;
206+
207+
console.log(a < b && b < c); // prints true to the console AND operator
208+
console.log(a < b || b > c); // prints true to the console OR operator
209+
console.log(!(a < b)); // prints false to the console NOT operator
210+
```
211+
212+
**Switch Case**
213+
```javascript
214+
var a = 1;
215+
216+
switch (a) {
217+
case 1:
218+
console.log("Value is 1");
219+
break;
220+
case 2:
221+
console.log("Value is 2");
222+
break;
223+
default:
224+
console.log("Value is not 1 or 2");
225+
break;
226+
}
227+
```
228+
229+
## Loops
230+
231+
Loops are used to perform a set of operations multiple times. There are three types of loops in JS. The syntax of loops is similar to other compile time programming languages.
232+
233+
**For Loop**
234+
```javascript
235+
for (var i = 0; i < 10; i++) {
236+
console.log(i);
237+
}
238+
239+
// prints 0 to 9 to the console
240+
```
241+
242+
**While Loop**
243+
```javascript
244+
var i = 0;
245+
246+
while (i < 10) {
247+
console.log(i);
248+
i++;
249+
}
250+
251+
// prints 0 to 9 to the console
252+
```
253+
254+
**Do-While Loop**
255+
```javascript
256+
var i = 0;
257+
258+
do {
259+
console.log(i);
260+
i++;
261+
} while (i < 10);
262+
263+
// prints 0 to 9 to the console
264+
```
265+
266+
## Functions
267+
268+
The syntax of functions is given below, the function name should be in camelCase and this is a named function.
269+
270+
```javascript
271+
function functionName(param1, param2, ...) {
272+
// function body
273+
return value;
274+
}
275+
```
276+
277+
There is also one more type of function, which is an anonymous function. This function is assigned to a variable and the variable name is used to call the function.
278+
279+
```javascript
280+
var functionName = function(param1, param2, ...) {
281+
// function body
282+
return value;
283+
}
284+
```
285+
286+
## Prompts
287+
There are three types of prompts in JS, which are `alert`, `confirm` and `prompt`.
288+
289+
**Alert**
290+
Just displays a message to the user. It doesn't return any value to the user.
291+
292+
```javascript
293+
alert("Hello World");
294+
```
295+
296+
**Confirm**
297+
Displays a message to the user and returns a boolean value to the user. If the user clicks on the `OK` button, then it returns `true` to the user, else it returns `false`.
298+
299+
```javascript
300+
var result = confirm("Are you sure?");
301+
302+
if (result) {
303+
console.log("User clicked on OK");
304+
} else {
305+
console.log("User clicked on Cancel");
306+
}
307+
```
308+
309+
**Prompt**
310+
Displays a message to the user and also takes input from the user. It returns the input value to the user.
311+
312+
```javascript
313+
var name = prompt("Enter your name");
314+
315+
console.log(name);
316+
```
156317

0 commit comments

Comments
 (0)