TIL DAY.5 Javascript(let,const,var)

Dan·2020년 7월 23일
0

var,let,const 의 차이점은?

varfunction-scoped

let,const block-scoped 이다.

var num = 10;
var num = 20;

document.write(num);

let

  1. let은 변수 값을 정의하지 않으면 자동적으로 undefined 값을 갖는다.
let prices;
console.log(price);
//output : undefined
price = 350;
console.log(price);
//Output: 350
  1. let 값은 제정의를 통해 다른 값으로 변경 될 수 있다.
let meal = 'Enchiladas';
console.log(meal);
//Output: Enchiladas
meal = 'Burrito' ;
console.log(meal);
//Output: Burrito

const

  1. constant 의 줄임말로 변수값을 정의하면 절대 바뀌지 않는다.
const myName = 'Gilberto';
console.log(myName);
//Output: Gilberto

var과 string의 활용법

var로 변수값을 지정해주고 ``와 ${}을 활용해 아래와 같이 편리하게 사용할 수 있다.

var myName = "Daniel";
var myCity = 'Seoul';
console.log(`My name is ${myName}.My favorite city is ${myCity}`);
//OutPut: My name is Daniel. My favorite city is Seoul

typeof

typeof 를 활용하여 실시간으로 데이터타입을 알 수 있다.

let newVariable = 'Playing around with typeof.';

console.log(typeof newVariable);
newVariable = 1;
console.log(typeof newVariable);

//output: string
number
profile
만들고 싶은게 많은 개발자

0개의 댓글