Day029_JavaScript

RISK_TAKER·2023년 3월 13일
  • js
    vs code extension: Code Runner install
    node js install -> 설치확인 cmd -> node --version
  1. 내부 스크립트 방법
    html 파일 내부의 body 태그가 끝나는 곳 이전에
    script 태그 내부에작성
    웹브라우저에 화면이 출력되는 것에 영향이 없도록 하기 위한 것
  2. 외부 스크립트 방법
	<script src="js 파일 위치"></script>
  1. 로그 출력하기 : console.log("로그 내용");

  2. 오류 확인 방법
    의심되는 코드 앞뒤에 로그를 작성하여 확인하기

  • var, let, const

  • 호이스팅
    변수의 선언하고 할당했을 경우 변수의 선언을 자바스크립트의 scope 맨 위로 올려실행하는 것이다.
    아래 코드의 경우 에러가 나지 않는다.
    : 의도하지 않게 값을 덮어쓰는 등의 문제가 있을 수 있다. 이때문에 var 대신에 let을 쓴다.

	console.log(numv); // 호이스팅때문에 에러가 나지 않는다.
    var numv = 400;
    console.log(numv);
    numv = 500;
    console.log(numv);
	// 중복선언을 해도 에러가 나지 않는다.
    var numv = 1000;
    console.log(numv);
  • let으로 숫자형(정수, 실수)이나 문자열, 논리형을 저장할 수 있다.

  • 백틱(`)을 활용하여 문자열 출력

	let s1 = "대한민국";
    let s2 = "천안시";
    let s3 = "교육실";
    console.log(s1 + "충남" + s2 + "휴먼" + s3);

    console.log(`${s1} 충남 ${s2} 휴먼 ${s3}`);
  • 자료형
    // { } 객체형태
    // [ ] 배열형태

    • 객체
            let stu1 = { //객체, key:value
              stuid:12345,
              name:'이이름',
              age:15
            };
    • 배열
    		let scores = [10, 20, 30, 40, 50];
  • 비교 연산자(==, ===)
    ==: 값이 같으면 true를 반환
    ===: 값과 자료형까지 같으면 true를 반환

	let num100 = 100;
	let str100 = '100';

	console.log(num100 == str100); //true
	console.log(num100 === str100); //false
  • 형변환
	let num100 = 100;
	let str100 = '100';
	
	console.log(num100.toString);
	console.log(String(num100));
	console.log(Number(str100));	
  • 반복문, for...in, of
for(let idx in arr){ //for in
    console.log(idx + " " + arr[idx]);
}

for(let val of arr){ //for of, 자바의 향상된 for문과 기능이 같다.
    console.log(val);
}
for(let i=0; i<arr.length; i++){
    console.log(arr[i]);
}

let person = {
    name:'김이름',
    age:30,
    phone:"01012345678"
};

for(let key in person){
    console.log(key + " " + person[key]);
}
  • 함수, function
    어떤 목적을 가지고 작성한 코드를 모아 둔 블록문
  • 함수를 정의하는 방법
	function gugudan3(){
        for(let i=1; i<=9; i++){
            console.log(`3 * ${i} = ${3*i}`);
        }
    }
    gugudan3(); //function 호출

    const funcGugudan4 =  function gugudan4(){
        for(let i=1; i<=9; i++){
            console.log(`4 * ${i} = ${4*i}`);
        }
    }
    funcGugudan4();

    const funcGugudan5 =  function(){
        for(let i=1; i<=9; i++){
            console.log(`5 * ${i} = ${5*i}`);
        }
    }
    funcGugudan5();

    const funcGugudan6 = () => {
        for(let i=1; i<=9; i++){
            console.log(`6 * ${i} = ${6*i}`);
        }
    }
    funcGugudan6();

0개의 댓글