const point = 1
const arr = []
const myObject = {}
const yourObject = myObject
예시) 다음과 같은 숫자, 문자열, boolean, null, undefined 등
const num = 2
const str = "자바스크립트"
const isFemail = true
const type = null
const str2 = str
특징
예시) 다음과 같은 object, array, function 등
const obj = {a: 1, b: 2}
const arr = [1, 2, 3, 4, 5, 6]
const func = () => {
console.log('function call')
}
const arr2 = arr
특징
// call by value
let myScore = 80
let yourScore = myScore
console.log(myScore) // 80
console.log(yourScore) // 80
myScore = 100
console.log(myScore) // 100
console.log(yourScore) // 80

let yourScore = myScore
myScore = 100
console.log(myScore) => 100
console.log(yourScore) => 80
// call by reference
let order = [1, 2, 3, 4, 5]
let anotherOrder = order
console.log(order) // [1, 2, 3, 4, 5]
console.log(anotherOrder) // [1, 2, 3, 4, 5]
anotherOrder[0] = 5
console.log(order) // [5, 2, 3, 4, 5]
console.log(anotherOrder) // [5, 2, 3, 4, 5]

let anotherOrder = order 
anotherOrder[0] = 5 


(장점): 편하다
(단점): 메모리가 부족할수 있다



y = f(x)함수 호출) 함수에게 알려줘야 하는 값 (매개변수)이 있고 함수는 특정 목적의 작업을 실행 후 결과를 반환한다.
함수의 원형 (prototype)을 만든다는 뜻
즉, 함수를 사용하기 위해 내용을 정의하는 행동
function sumNumber(a, b) {
return a + b
}
const multiplyNumber = (a, b) => {
return a * b
}
선언된 함수를 실제로 사용하는 행동
sumNumber(1, 2) // => 3
multiplyNumber(3, 4) // => 12
function sumNumber(a, b) {
return a + b
}
function getMovieData(searchWord) {
.....
}const multiplyNumber = (a, b) => {
return a * b
}
arr.filter(**() => {
....
}**);
const squareNum = function(num) {
return num * num
}함수 선언식함수 표현식 이라 합니다.function sumNumber(a, b) {
return a + b
}
function getMovieData(searchWord) {
.....
}호이스팅의 영향을 받아 함수 선언부가 스코프의 시작점으로 끌어 올려지는 현상 발생func() // console.log 출력 됨
function func() {
console.log('func function call!!!!')
}const multiplyNumber = (a, b) => {
return a * b
}
const squareNum = function(num) {
return num * num
}func() // 에러 발생 (ReferenceError: Cannot access 'func' before initialization)
const func = () => {
console.log('func function call!!!!')
}실행 컨텍스트 등록 단계초기화 단계let num = 1
할당 단계let num = 1
스코프의 최상단으로 끌어올리는 현상 (선언이 끌어올려지는 것임)// case 1
func() // console.log 출력 됨
function func() {
console.log('func function call!!!!')
}//case 2
func() // 에러 발생 (ReferenceError: Cannot access 'func' before initialization)
let func = () => {
console.log('func function call!!!!')
}
// Step 2 (메모리에 공간을 할당받고 undefined)
let func = undefined
// Step 3 func에 함수를 할당
func = () => {
console.log('func function call!!!!')
}var 키워드로 변수를 선언한 경우Step 1) 실행 컨텍스트 등록 + (Step 2) 초기화까지 실행된 상태로 호이스팅 됩니다.function func() {
console.log(num) // undefined 출력
var num = 1
}
func()함수 스코프를 갖게 됩니다.function func() {
const num = 1
if (num === 1) {
var hoistingVar = '호이스팅 잘되나요?'
}
console.log(hoistingVar) // 호이스팅 잘되나요? 출력
}
func()let, const 키워드로 변수를 선언한 경우Step 1) 실행 컨텍스트 등록 상태까지만 진행되고 호이스팅 됩니다.function func() {
console.log(num) // ReferenceError: Cannot access 'num' before initialization
let num = 1
}
func()블록 스코프를 갖게 됩니다.function func() {
const num = 1
if (num === 1) {
let hoistingVar = '호이스팅 잘되나요?'
}
console.log(hoistingVar) // ReferenceError: hoistingVar is not defined
}
func()함수 스코프함수가 됨function func1() {
**// (1)**
function func2() {
**// (2)**
const num = 1
if (num === 1) {
**// (3)**
var hoistingVar = '호이스팅 잘되나요?' // 어디까지 끌어 올려질까요?
}
console.log(hoistingVar)
}
}
func1()블록 스코프블록이 됨{} 로 열고 닫는 영역의 범위function func1() {
**// (1)**
function func2() {
**// (2)**
const num = 1
if (num === 1) {
**// (3)**
let hoistingVar = '호이스팅 잘되나요?' // 어디까지 끌어 올려질까요?
}
console.log(hoistingVar)
}
}
func1()내부함수에서는 외부함수 스코프에서 선언된 변수에 접근 기능하다.function init() {
var name = "Mozilla"; // name은 init에 의해 생성된 지역 변수이다.
function displayName() { // displayName() 은 내부 함수이며, 클로저다.
alert(name); // 부모 함수에서 선언된 변수를 사용한다.
}
displayName();
}
init();