2. Variables and Functions
2. Variables and Functions
js
1
2
3
4
Variables{
5
6
[in JavaScript]
7
8
9 Echo ‘Explain JavaScript Variables’;
10
11
12
13 }
14
JavaScript Variables
variables.html variables.js
1
2
4 types of declaring a variable
3 Automatically
4 Using var
5
Using let
6
7
Using const
8
9
10
11
12
13
14
JavaScript Variables
variables.html variables.js
1
2
Variable Names
3 ∗ Begin with letters, $ or _
4 ∗ Only contain letters, numbers, $ and _
5 ∗ Case sensitive
6
∗ Avoid reserved words
7
8
∗ Choose clarity and meaning
9 ∗ Prefer camelCase for multipleWords (instead of
10 under_score)
11 ∗ Pick a naming convention and stick with it
12
13
14
JavaScript Variables
variables.html variables.js
1
2
Loosed Typing
3 you don't have to specify what type of information will be
4 stored in a variable in advance
5
6
7
8
9
10
11
12
13
14
JavaScript Variables
variables.html operators.js
1
2
Operators
3
4
5
6
7
8
9
10
11
12
13
14
JavaScript Variables
variables.html operators.js
1
2
Assignment
3
4
5
6
7
8
9
10
11
12
13
14
JavaScript Variables
variables.html datatypes.js
1
2
Data Types
3
4 String
5
6
7 Boolean
8
9
10 Number
11
12
13
14
JavaScript Variables
variables.html concat.js
1
2
Concat()
3 The variable should be declared as string before you can
4 use concat
5
6
7
8
9
10
11
12
13
14
JavaScript Functions
variables.html concat.js
1
2
You can do this too..
3 ∗ You can directly concat strings without using concat()
4 function
5 ∗ Using , and + have the same output
6
7
8
9
10
11
12
13
14
JavaScript Functions
variables.html functions.js
1
2
Functions
3
4
5
6
7
8
9
10
11
12
13
14
JavaScript Functions
variables.html functions.js
1
2
Function Call
3 Without parameter:
4
5
6
7
8 With parameter:
9
10
11
12
13
14
JavaScript Functions
variables.html functions.js
1
2
Beware: Circular Dependencies
3 This snippet will be stuck in an infinite loop
4
5
6
7
8
9
10
11
12
13
14
JavaScript Functions
variables.html functions.js
1
2
Arguments
3 Functions can accept any number of named arguments:
4
5
6
7
8
9
10
11
12
13
14
JavaScript Functions
variables.html functions.js
1
2
Return Values
3 The return keyword returns a value to whoever calls the
4 function(and exits the function):
5
6
7
8
9
10
11
12
13
14
JavaScript Functions
forbeginners.html varxfunc.css
1
2
3
4
5
6 < “Now you understand how function and
7 variables works, let’s combine them and find
8 out how they work together.>
9
10 — your handsome prof
11
12
13
14
1
2
Variable Scope
JS Variables have “Function scope”. They are visible in the
3
function where they’re defined:
4
5
A variable
6 with local
7 scope:
8
9
10
11 A variable
12 with global
13 scope:
14
1
2
Conditional
3 Operators
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6 < “Now you understand how conditional
7 operators works, let’s apply them in an if…
8 else statement.>
9
10 — your handsome prof
11
12
13
14
Conditions
variables.html conditions.js
1
2
Now we can proceed…
3
4
5
6
7
8
9
10
11
12
13
14
Conditions
variables.html conditions.js
1
2
Don’t forget…
3
4
5
6
7
8 == is not the same with =
9
10
11
12
13
14
Conditions
variables.html conditions.js
1
2
3
4
5
6
7
8
9
Thank you!
10
11
12
13
14
Conditions