본문 바로가기
javascript

변수, type, 할당, return

by rami_ 2022. 1. 23.

<head>

<script defer src= "main.js"></script>

</head>

 

script defer로 작성하는 것이 제일 좋음. HTML을 parsing하는 동안 다운받고 페이지가 준비되면 실행함.

여러개일 경우 순서대로 실행됨.

 

 

자바스크립트에서 중요한것 : 입력/연산/출력

 

변수(var는 blcok scope가 없음. 사용하지 않음)

1. let (mutable)

2. const 값이 정해지면 바꿀 수 없음. (immutable)

 

variable type

1. primitive, single item : number, string, boolean, null, undefined, symbol

2. object(primitive type을 제외하고 모두), box container

3. function, first-class function

 

boolean

 false : 0, null, undefined, NaN, ' '

 true : any other value

 

null : 아무것도 없는 값

undefined : 선언은 되었지만 값이 없음.

 

object : real-life object, data structure 

const ellie = { name : 'ellie', age : 20};

ellie.age = 20;

ellie.name = 'eliie';

 

 

symbol : create unique identifiers for objects

const symbol1 = Symbol('id');

const symbol2 = Symbol('id');

symbol1 === symbol2; //fasle

 

const symbol1 = Symbole.for('id');

const symbol2 = Symbole.for('id');

symbol1 === symbol2; //true

 

console.log(`value : ${symbol1.description}, type: ${typeof(symbol)}`);

//value: id , type : symbol

 

dinamic typing : dynamically typed langauge

 

 

= assignment(할당)

+= add assignment(더하기 할당)

let a = 3;

a += 2; // 3(a)과 2를 더해 다시 a에 할당.

 

== equality type이 다를 경우 타입을 바꿔 맞는지 보려고 함.

"1" == 1 //true

const number1 = new Number(3);

const numebr2 = new Number(3);

number1 == 3; //true

number1 == number2; //false

 

const object1 = {"key" : "value"};

const object2 = {"key" : "value"};

object1 == object2 //false

object2 == object2 //true

 

const string1 = "hello";

const string2 = String("hello");

const string3 = new String("hello");

const string4 = new String("hello");

string1 == string2 //true

string1 == string3 //true

string2 == string3 //true

string3 == string4 //false

string4 == string4 //true

 

 

=== stric equality

 

 

return 함수실행을 종료하고 함수 호출자에게 반환될 값을 지정함.

ends function excution and specifies a value to be returned to the function caller.

return [expression] expression이 생략되면 undefined를 반환함.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


백틱 작성법 : 1왼쪽에 있는 키

 

 

 

'javascript' 카테고리의 다른 글

First-class Function  (0) 2022.01.24
closure  (0) 2022.01.23
기본개념5. Object  (0) 2022.01.09
기본개념 4 Number 오브젝트 프로퍼티  (0) 2022.01.09
기본개념 3 Object  (0) 2022.01.07