22-04-26 JavaScript

SOMEmo·2022년 4월 26일
0

$ npm init -y
$ npm i parcel-bundler -D
-package.json
-test부분 지우고 거기에
"dev": "parcel index.html",
"build": "parcel build index.html"
-새파일-index.html
-파일수정-npm run dev

  1. 데이터 타입 확인
console.log(typeof []);

function getType(data) {
  return Object.prototype.toString.call(data).slice(8, -1);
}

내보내기

export default function getType(data) {
  return Object.prototype.toString.call(data).slice(8, -1);
}

가져오기

import getType from "./getType.js";
  1. 비교, 논리 연산자

!== 불일치 연산자
논리연산자 &&그리고and
||또는or
!부정not

  1. 삼항 연산자
    console.log(a ? '참' : '거짓')

  2. 조건문 if else
    export default function random() {
    return Math.floor(Math.random() * 10);
    }

import random from './getRandom'

if () {
} else if () {
} else {
}

  1. 조건문 Switch
const a = random();
switch (a) {
  case 0:
    console.log("a is 0");
    break;
  case 2:
    console.log("a is 2");
    break;
  case 4:
    console.log("a is 4");
    break;
  default:
    console.log("rest...");
}
  1. 반복문 For
//for(시작조건; 종료조건; 변화조건) {}

for (let i = 0; i <3; i += 1) {
  
}

li 추가

const ulEl = document.querySelector("ul");

for (let i = 0; i < 3; i += 1) {
  const li = document.createElement("li");
  li.textContent = `list-${i + 1}`;
  ulEl.appendChild(li);
}
for (let i = 0; i < 3; i += 1) {
  const li = document.createElement("li");
  li.textContent = `list-${i + 1}`;
  if ((i + 1) % 2 === 0) {
    li.addEventListener("click", function () {
      console.log(li.textContent);
    });
  }
  ulEl.appendChild(li);
}
  1. 변수 유효범위
    let,const는 선언된 블록범위{}가 유효범위.
    var는 함수레벨의 유효범위

  2. 형 변환
    === 일치연산자
    == 동등연산자

//Truthy(참 같은 값)
true, {}, [],, 1, 2, 'false', -12, '3.14' ...

//Falsy(거짓 같은 값)
//false, '', null, undefined, 0, -0, NaN

0개의 댓글