소프트웨어 교육

[javascript] 변수

지구빵집 2022. 12. 30. 09:54
반응형

 

 

 

 

 


let globalName = 'global name';
{
  let name = 'apple';
  console.log(name);
  name = 'hello';
  console.log(name); //
  console.log(globalName);
}

console.log(name); //실행 안됨.
console.log(globalName);

결과는 

mahall
hello
lobal name

lobal name

 

 

 

 

// var는 사용하지 말고
// var hoisting 이란 선언을 가장 위로 끌어올리는 일
// var 는 블럭을 무시함. 스코프 무시함.
{
  age = 4;
  var age;
}
console.log(age);

결과

4

 

 

// 값이 바뀌지 않는 변수 - 상수 
// 가능하면 값을 변경할 수 없는 상수를 사용
// 변할 필요가 없다면 사용

// 뮤터블 타입 let, 이뮤터블 타입 const

const daysInWeek = 7;
const maxNumber = 5;

 

 

 

데이터 타입

// primitive, single item: number, string, boolean, null, undefined, symbol
// object, box container
// function, first-class function(변수 할당 가능, 인자로 전달 가능, 리턴도 가능)

// number
const count = 17; // integer
const size = 17.1; // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);

결과

value: 17, type: number
value: 17.1, type: number

// number - speicla numeric values: infinity, -infinity, NaN
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = "not a number" / 2;
console.log(infinity);
console.log(negativeInfinity);
console.log(nAn);

결과

Infinity
-Infinity
NaN

// 최근 추가 bigInt (fairly new, don't use it yet)
const bigInt = 1234567890123456789012345678901234567890n; // over (-2**53) ~ 2*53)
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);

결과

value: 1234567890123456789012345678901234567890, type: bigint

// string
const char = "c";
const brendan = "brendan";
const greeting = "hello " + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`; //template literals (string)
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
console.log("value: " + helloBob + " type: " + typeof helloBob);

결과

value: hello brendan, type: string
value: hi brendan!, type: string
value: hi brendan! type: string


// boolean
// false: 0, null, undefined, NaN, ''
// true: any other value
const canRead = true;
const test = 3 < 1; // false
console.log(`value: ${canRead}, type: ${typeof canRead}`);
console.log(`value: ${test}, type: ${typeof test}`);

결과

value: true, type: boolean
value: false, type: boolean

// null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

결과

value: null, type: object

// undefined
let x;
console.log(`value: ${x}, type: ${typeof x}`);

결과

value: undefined, type: undefined

// symbol, create unique identifiers for objects
const symbol1 = Symbol("id");
const symbol2 = Symbol("id");
console.log(symbol1 === symbol2);
const gSymbol1 = Symbol.for("id");
const gSymbol2 = Symbol.for("id");
console.log(gSymbol1 === gSymbol2); // true
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);

결과

false
true
value: id, type: symbol

 

 

// 5. Dynamic typing: dynamically typed language
let text = "hello";
console.log(text.charAt(0)); //h
console.log(`value: ${text}, type: ${typeof text}`);
text = 1;
console.log(`value: ${text}, type: ${typeof text}`);
text = "7" + 5;
console.log(`value: ${text}, type: ${typeof text}`);
text = "8" / "2";
console.log(`value: ${text}, type: ${typeof text}`);
console.log(text.charAt(0)); // Error 숫자 타입으로 변경되어서

결과

h
value: hello, type: string
value: 1, type: number
value: 75, type: string
value: 4, type: number


// object, real-life object, data structure

const kim = { name: "mashall", age: 24 };
ellie.age = 21;

 

 

정리한다. 

 

 

자바스크립트 변수

 

 

반응형