# what is javascript? II
## function and let, var, const relationship
1. all of function has return value. if you not write return it will be like this
"Return NormalCompletion(undefined)."
2. parameters vs arguments
```javascript=
var foo = function( a, b, c ) {}; // a, b, and c are the parameters
foo( 1, 2, 3 ); // 1, 2, and 3 are the arguments
```
3. function rule
```javascript=
function A(){
function B(){
console.log("B")
}
}
A() //nothing happen
B() //b is not defined
function A(){
function B(){
console.log("B")
}
B()
}
A() //b
```
it will not error
```javascript=
function A(a,b){
console.log(a,b)
}
A(1,2,3) //1,2
```
one of method to avoid
```javascript=
function A(a=1,b=2,c=3){
console.log(a,b,c)
}
A(1,2,3) //1,2
```
order of precedence
Variable assignment over function declaration
```javascript=
var double = 22;
function double(num) {
return (num*2);
}
console.log(typeof double); // Output: number
```
Function declarations over variable declarations
```javascript=
var double;
function double(num) {
return (num*2);
}
console.log(typeof double); // Output: function
```
function hoisting
https://www.digitalocean.com/community/tutorials/understanding-hoisting-in-javascript
3. anonymous function
this function is no necessary function name, kind of this using case about ...
https://www.geeksforgeeks.org/javascript-anonymous-functions/
```javascript=
var a = function(){ console.log("hi")}
a() //hi
var b = function(x){ console.log(x)}
b("c") // c
```
4. arrow function
like a anonymous function, but it other way to display
like that ... https://www.w3schools.com/js/js_arrow_function.asp
```javascript=
var foo = (x,y,n)=>{};
```
5. scope chain
javascipt has 3 kind of type always use in script .. has different scope
```javascript=
d = 4 // this is global type
//limit in function
function A(){
var a;
}
//limit in block
{
let b = 1;
const c = function(){}
}
```
6. rule of them's declare
```javascript=
var a = 1
var a = 2
var a
let b = 3
let b = 4 //error cant same name
let e; //error cant declare no assign value
const a = 4
a=5 //error after assign cant change
```
7. about type of declare and execute
```javascript=
function A(){
console.log(a)
var a = 3 //print undefined
}
function B(){
console.log(b)
let b = 4 //print ReferenceError it mean "temporal dead zone"
}
function C(){
console.log(c)
const c = function(){
console.log("c")
} //print ReferenceError it mean "temporal dead zone"
}
```
8. rule of global
because function A will find b, if cant find, will find outside
it mean will change outside value, like this example
```javascript=
let b = 1
function A(){
b = 3
}
A()
console.log(b) //3
```
in this example, becuase run function A will run function B, it will find a
but in function cant find, so B go outside to find a, cause change function A's a value, after B function already find a, it will not continue find a more!
```javascript=
var a = "123"
function A(){
var a = 4
function B(){
a = 3
console.log(a)
}
B()
console.log(a)
}
A()
console.log(a)
//3
//3
//123
```
in this case, console.log(a) a is assgin by var a, it same, let a it limit on black
so ans is undefind
```javascript=
if(false){
var a=1
}
console.log(a)
if(false){
let a=1
}
console.log(a) //a is var a!!!not a let a!!!
```
that will explanation
```javascript=
if(false){
var a=1
}
console.log(a)
if(false){
let b=1
}
console.log(b) //b is not defind
```
how about it? because i it var it not limit so console.log can get it, 5 is end of for loop i++
```javascript=
for(var i = 0; i<5; i++){
}
console.log(i) //i is 5
```
###### tags: `JavaScript`