1
+ // 1. Functions
2
+
3
+ function myname ( ) {
4
+ console . log ( "A" ) ;
5
+ console . log ( "y" ) ;
6
+ console . log ( "u" ) ;
7
+ console . log ( "s" ) ;
8
+ console . log ( "h" ) ;
9
+ }
10
+ myname ( ) /* myName is reference of the function & () is execution of the function.
11
+
12
+ ---------------------------------------------------------------------------------------*/
13
+
14
+ function addTwoNumbers ( number1 , number2 ) { // Here, number1 & number2 are Parameters.
15
+ console . log ( number1 + number2 ) ;
16
+ }
17
+ // const result = addTwoNumbers(3, 4) // and Here, 3 & 4 are Arguments.
18
+ /* Output : 7
19
+ But if we check the value in result variable, */
20
+ // console.log(`Result : ${result}`); // Output : Result : undefined
21
+ /* Printing console in function doesn't means it's returning the value.
22
+
23
+ To fix this,*/
24
+ function addTwoNumbers2 ( number1 , number2 ) {
25
+ // let result = number1 + number2
26
+ // return result
27
+ /* console.log("Ayush"); This will never be excecuted cause after return function
28
+ doesn't work. */
29
+ }
30
+ // const result = addTwoNumbers2(3, 4)
31
+ // console.log(result); // Output: 7
32
+
33
+ // One more method of doing the same,
34
+ function addTwoNumbers3 ( number1 , number2 ) {
35
+ return number1 + number2
36
+ }
37
+ const result = addTwoNumbers3 ( 3 , 10 )
38
+ console . log ( result ) ; /* Output: 13
39
+
40
+ ---------------------------------------------------------------------------------------*/
41
+
42
+ // (i)
43
+ function userLogin ( username ) {
44
+ return `${ username } just logged in.`
45
+ }
46
+ // console.log(userLogin("Ayush")); // Output: Ayush just logged in.
47
+
48
+ // (ii)
49
+ function userLogin2 ( username ) {
50
+ if ( ! username ) { // (!username) states (username === undefined)
51
+ console . log ( "Please enter a username." ) ;
52
+ return /* if "if" statement will be true then return will stop
53
+ code after the above console.log in this function. */
54
+ }
55
+ return `${ username } just logged in.`
56
+ }
57
+ // console.log(userLogin2()); // Output: undefined
58
+ // Output: Please enter a username.
59
+ // console.log(userLogin2("Ayush"));
60
+ // Output: Ayush just logged in.
61
+
62
+ // (iii)
63
+ function userLogin3 ( username = "sam" ) {
64
+ return `${ username } just logged in.`
65
+ }
66
+ console . log ( userLogin3 ( ) ) ; // Output: sam just logged in.
67
+ console . log ( userLogin3 ( "Ayush" ) ) ; // Output: Ayush just logged in.
0 commit comments