1
+ function sayMyName ( ) {
2
+ console . log ( "R" ) ;
3
+ console . log ( "O" ) ;
4
+ console . log ( "N" ) ;
5
+ console . log ( "Y" ) ;
6
+ }
7
+
8
+ sayMyName ( ) // exceution of function
9
+ sayMyName // reference of function
10
+
11
+ /* When you write the function definition and you take any inputs in that those
12
+ are called Function Parameters
13
+
14
+ Here in addTwoNumbers -> number1 and number 2 are the parameters of the function.
15
+
16
+ */
17
+
18
+ function addTwoNumbers ( number1 , number2 ) {
19
+ console . log ( number1 + number2 ) ;
20
+ }
21
+
22
+ addTwoNumbers ( 3 , 4 ) // o/p-> 7. Notice that , 3 and 4 are the arguements of the function name addTwoNumbers
23
+
24
+ const result = addTwoNumbers ( 3 , 4 )
25
+ console . log ( "Result :" , result ) ; // o/p -> Result : undefined
26
+
27
+ /*
28
+ Notice that you are trying to hold the output of addTwoNum in variable - result.
29
+ But when you run the code, you get that Result has stored undefined.
30
+
31
+ Why? - Because addTwoNum() is just printing the addition and not returning the result.
32
+ That's why undefined is printed.
33
+ */
34
+
35
+ function addThreeNumbers ( number1 , number2 , number3 ) {
36
+ const result2 = number1 + number2 + number3
37
+ return result2
38
+ console . log ( " Print me if you can? " ) // This will never print as there is a rule in function that after return statement, code is undetectable.
39
+ }
40
+
41
+ const result2 = addThreeNumbers ( 5 , 10 , 10 )
42
+ console . log ( "Result of addition of Three Numbers:" , result2 ) ; // Result of addition of Three Numbers: 25
43
+ /*
44
+ Now you can get the value stored in the variable result 2 as the
45
+ function addThreeNumbers is now returning the value as result2
46
+ */
47
+
48
+
49
+
50
+ /*
51
+ If you are thinking that only returning from a variable will give the result,
52
+ it's not, you can directly return and get the function result stored in the variable. */
53
+
54
+ function addFourNumbers ( number1 , number2 , number3 , number4 ) {
55
+ return number1 + number2 + number3 + number4
56
+ }
57
+
58
+ const res3 = addFourNumbers ( 10 , 10 , 20 , 20 )
59
+ console . log ( "Result of four no. addition :" , res3 ) ; // o/p-> Result of four no. addition : 60
60
+
61
+
62
+
63
+ function loginUserMessage ( username ) {
64
+ return `${ username } just logged in`
65
+ }
66
+
67
+ console . log ( loginUserMessage ( "Sandeepan" ) ) // o/p-> Sandeepan just logged in
68
+
69
+ // if you don't pass any arguement to the loginUserName function, the output is undefined
70
+
71
+ console . log ( loginUserMessage ( ) ) // o/p-> undefined just logged in, that's why we need validation.
72
+
73
+
74
+ // Consider the below example of usecase of validation :-
75
+
76
+ function loginUserMessage2 ( username ) {
77
+ //another way of putting validation :-
78
+ // if(!username){
79
+ // console.log("provide a username");
80
+ // return
81
+ // }
82
+ if ( username === undefined ) {
83
+ console . log ( "provide a username" ) ;
84
+ return /* This return will execute when no username is provided and terminate the code there only
85
+ and will print the o/p-> provide a username
86
+ */
87
+ }
88
+ return `${ username } just logged in`
89
+ }
90
+
91
+ console . log ( loginUserMessage2 ( ) ) // o/p-> undefined
92
+
93
+
94
+ /* One more thing, we can by default also assign a username to the function.
95
+
96
+ For understanding it better, consider the function loginUserMessage3 in the below.
97
+
98
+ */
99
+
100
+ function loginUserMessage3 ( username = "Sam" ) {
101
+ //another way of putting validation :-
102
+ // if(!username){
103
+ // console.log("provide a username");
104
+ // return
105
+ // }
106
+ if ( username === undefined ) {
107
+ console . log ( "provide a username" ) ;
108
+ return /* This return will execute when no username is provided and terminate the code there only
109
+ and will print the o/p-> provide a username
110
+ */
111
+ }
112
+ return `${ username } just logged in`
113
+ }
114
+
115
+ console . log ( loginUserMessage3 ( ) ) // o/p-> Sam just logged in ( because the if username will never be undefined as Sam is by default parameter)
116
+
117
+ console . log ( loginUserMessage3 ( "Virat kohli" ) ) // o/p-> Virat kohli just logged in ( The parameters are easily overriden.)
0 commit comments