본문 바로가기

소프트웨어 교육

[javascript] 변수

반응형

 

 

 

 

 


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;

 

 

정리한다. 

 

 

자바스크립트 변수

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

귀사가 성공하기까지의 긴 고난의 시간을 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩