HTML,CSS,JS
JS 기초편(1)
seung_ho_choi.s
2023. 6. 25. 22:54
728x90
개요: 필자가 공부하면서 정리한 게시물이다.
<변수>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>승호</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
html 파일에 js를 넣고 싶으면 <script> 문법을 이용해 다음과 같이 작성한다.
alert("hi");
해당 js 파일 소스는 다음과 같다.
결과는 이렇다!
콘솔로도 제어가 가능하다!
문자로 할때는 ""붙여서 한다! string으로 인식이 된다.
console.log(1234156)
console.log를 하면 콘솔에 log 또는 print하는 일을 한다.
const a=5;
const b=2;
console.log(a%b);
console.log(a+b);
console.log(a*b);
const 변수를 써서 다음과 같이 지정이 된다.
const myName="승호";
let myName1="승호2";
이때 let 이라는 변수도 쓸 수 있는데 둘의 차이점은 변경 가능하냐 변경 불가능하냐 차이다.
즉 변수를 업데이트 하고 싶을때 let을 사용하면 된다. var은 쓰지말자!
const amIFat2=false;
const amIFat1=true;
boolean 타입도 있다. 이것의 사용예시로는 사용자 로그인, 비디오 재생, 웹사이트 로딩 이 있다.
let something;
console.log(something);
다음과 같이 something이 정의가 안되어 있으면 undefined 라고 뜬다.
const abc=null;
console.log(abc);
다음과 같이 null로도 나타낼 수 있다.
즉 const, let, flase, true, null, undefined 타입이 있다.
<배열>
const mon="mon";
const tue="tue";
const wed="wed";
const thu="thu";
const today=[mon,tue,wed,thu];
console.log(today);
다음과 같이 배열로 하면 string과 다르게 저렇게 뜬다.
console.log(today[3]);
원하는 요소 출력 방법은 자바랑 똑같다!
today.push("aa");
console.log(today);
push를 통해 추가할 수 있다. 자바랑 그냥 똑같다.
<Object>
const player={
name: "최승호",
points: 10,
fat: true,
};
console.log(player.fat);
console.log(player.name);
console.log(player.points);
console.log(player);
c언어 구조체저럼 다음과 같이 한번에 정의해서 쓸수 있다. 문법은 위와 같다. 자바랑 많이 비슷하다.
이때 안에 안에 있는 내용들 let 속성이라서 변경 가능하다!
player.lastName="d";
player 안에 정의되지 않은애들도 저렇게 추가가 가능하다.
728x90