FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/JS Basics/scripts/functions.js at master · Ch-sriram/JavaScript · GitHub
Ch-sriram
/
JavaScript
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
JS Basics
/
scripts
/
functions.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (81 loc) · 3.54 KB
Breadcrumbs
JavaScript
/
JS Basics
/
scripts
/
functions.js
Copy path
File metadata and controls
94 lines (81 loc) · 3.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/********************************************************************************************
* Functions: Declarations, Statements & Expressions.
*/
// Function Declaration
function
calculateAge
(
YOB
)
{
// YOB: Year of Birth
var
currYear
=
2019
;
return
currYear
-
YOB
;
}
// Function Statement is calling the function after it was defined
var
johnAge
=
calculateAge
(
1990
)
;
var
ramkiAge
=
calculateAge
(
1955
)
;
var
ramAge
=
calculateAge
(
1995
)
;
console
.
log
(
"John's Age: "
+
johnAge
+
"; "
+
"Ramki's Age: "
+
ramkiAge
+
"; "
+
"Ram's Age: "
+
ramAge
+
";"
)
;
// Function Declaration that doesn't return anything
function
yearsUntilRetirement
(
birthYear
,
firstName
)
{
var
age
=
calculateAge
(
birthYear
)
;
var
retirement
=
58
-
age
;
// Retirement Age in India is 58
var
retirementStatus
=
retirement
<
0
?
"retired"
:
"not retired"
;
if
(
retirementStatus
===
"not retired"
)
console
.
log
(
firstName
+
" still has "
+
retirement
+
" years for retirement"
)
;
else
console
.
log
(
"It has been "
+
(
-
retirement
)
+
" years since "
+
firstName
+
" has retired"
)
;
}
yearsUntilRetirement
(
johnAge
,
"John"
)
;
yearsUntilRetirement
(
ramkiAge
,
"Ramki"
)
;
yearsUntilRetirement
(
ramAge
,
"Ram"
)
;
// Function Declaration is as follows:
// function occupation(job, firstName) {}
// Same as the declarations as we have seen above.
// But, Function Expressions are anonymous functions that are assigned to a JS variable/object as follows:
var
occupation
=
function
(
job
,
firstName
)
{
var
doesThis
=
""
;
switch
(
job
)
{
case
"teacher"
:
doesThis
+=
"is a teacher who teaches how to code"
;
break
;
case
"driver"
:
doesThis
+=
"is a truck driver"
;
break
;
case
"coder"
:
case
"software engineer"
:
doesThis
+=
"is a software engineer working at xyz company"
;
break
;
case
"designer"
:
doesThis
+=
"is a system designer working for xyz company"
;
break
;
default
:
doesThis
+=
"does something else"
;
}
return
firstName
+
" "
+
doesThis
;
}
var
johnJob
=
"teacher"
,
ramkiJob
=
"retired"
,
ramJob
=
"coder"
;
console
.
log
(
occupation
(
johnJob
,
"John"
)
)
;
console
.
log
(
occupation
(
ramkiJob
,
"Ramki"
)
)
;
console
.
log
(
occupation
(
ramJob
,
"Ram"
)
)
;
/********************************************************************************************
* Expressions vs. Statements in JS
* --------------------------------
*
* In JS, Expressions return something explicit immediately, whereas statements do not return
* something immediately. The above anonymous function is assigned to occupation
* variable, it means that whenever we call occupation, we basically are expecting something
* in return. But whenever a statement like if(), while(), for(), etc are used, they'll
* obviously do something with the code, but they themselves never return anything explicit.
*
*
* statement example:
* > if(true) {console.log(45);}
* This is a statement. if() will return undefined. But since console.log(45) is an
* expression, we will get a return value of 23. If we copy and paste this line in
* the browser, we will get undefined & 45 as the return value.
*
* expression example:
* > occupation(johnJob, "John");
* This line here is an expression and definitely returns something. If we open the
* HTML page related to this JS page in the browser & open the console and copy-paste
* the line of code above, we will see that it returns some kind of a value, which
* is not undefined.
*/
Back
|
FazBrowse Home
|
New Git URL