본문 바로가기
Program Language/JavaScript

[JavaScript] 자바스크립트 기초 (1)

by QueryJun 2022. 11. 23.

const 와 let 의 차이

- const 는 상수(constant), 상수(constant)는 불변!

- let 은 선언 후 변경 가능

 

# 코드 예시

const a = 5;
const b = 2;
let myName = "jun";

Arrays

- 일반적인 배열 선언과 동일

 

#코드 예시

const daysOfWeek = ["mon", "tue", "wed", "thu", "fri", "sat"]
daysOfWeek.push("sun") // python의 append의 기능

Object

- property를 가진 데이터를 저장

const player = {
   name: "jun",
   points: 10,
   fat: true
};

player.height = 180 // object 추가 가능

console.log(player.name); // jun
console.log(player["name"]); // jun

Function

- 일반적인 함수 생성과 동일

- Object 안에 함수 생성도 가능

function plus(a,b){
console.log(a+b);
}

plus(10,20);

const player={
name : "jun",
sayHello: function(otherPersonsName){
console.log('hello ' + otherPersonsName + ' nice to meet you');
}
}

console.log(player);
player.sayHello('lynn');

Conditionals(조건문)

const age = parseInt(prompt('what is you age?'));

if (isNaN(age)){ //숫자인지 아닌 지 판별
console.log('please write a number');
}
else if (age<18){
console.log('you are too young');
}
else{
console.log('you can drink');
}
반응형