Scope , 원시자료형 vs 참조자료형

yejichoi·2022년 12월 23일
0
post-thumbnail

Scope

Scope determines the accessibility (visibility) of variables.

JavaScript has 3 types of scope:

  • Block scope

efore ES6 (2015), JavaScript had only Global Scope and Function Scope.

ES6 introduced two important new JavaScript keywords: let and const.

These two keywords provide Block Scope in JavaScript.

Variables declared inside a { } block cannot be accessed from outside the block:
Variables declared with the var keyword can NOT have block scope.

Variables declared inside a { } block can be accessed from outside the block.

  • Function scope

JavaScript has function scope: Each function creates a new scope.

Variables defined inside a function are not accessible (visible) from outside the function.

Variables declared with var, let and const are quite similar when declared inside a function.

  • Global scope

A variable declared outside a function, becomes GLOBAL.
Variables declared Globally (outside any function) have Global Scope.

Global variables can be accessed from anywhere in a JavaScript program.

Variables declared with var, let and const are quite similar when declared outside a block.

Reference : https://www.w3schools.com/js/js_scope.asp


Javascript Primitive type vs Reference type (원시자료형 vs 참조자료형)

원시타입(7)

변수에 값(value) 이 담김
String
Number
Boolean
Bigint
undefined
Symbol
null

값이 절때 변하지않는 불변성을 갖고있기때문에 때문에 재할당 시 기존 값이 변하는것 처럼 보일지 몰라도 사실 새로운 메모리에 재할당한 값이 저장되고 변수가 가리키는 메모리(주소)가 달라졌을 뿐

참조타입

변수에 할당할 때는 변수에 값이 아닌 주소를 저장
원시타입을 제외한 나머지(배열,객체,함수 대표적)

원시타입과 가장 큰 차이점은 변수의 크기가 동적으로 변함

0개의 댓글