Web/JS

js - 이론 #1

야이주 2021. 6. 7. 23:51

#2.0

시작하기

js 파일은 html의 <script>태그를 통해 열 수 있다

<script src="script.js">

#2.1

데이터타입

1. 숫자

  • 정수형 : 1, 2
  • 실수형 : 1.5

2. 문자 string

  • "hello"

3. boolean

참과 거짓을 나타내는 값

const amIFat = ture; //boolean값 true

const amIFat = "ture"; //x     문자열 값 true

4. null

아무 것도 없는 값

const amILat = null;

5. undefine

let something; //초기화를 하지 않음. undefine값 출력

#2.2, 2.3

console

console.log( 'lala' ); //콘솔에 lala 출력

 

변수 (valiable)

 1. const (상수)

const a = 5

 2. let (변수)

let b = 2;

3. var 

구 변수, 규칙이 없음

const를 기본적으로 사용하고 변경해야 하는 변수에만 let을 사용. var는 사용하지 말 것.

 

변수 작명법

  • camelCase(낙타 등) : 자바스크립트 코드 작성법
const name = veryLongVariableName

 

  • snake_case(뱀 모양) : 파이썬 ,자바 등의 코드 작성법
const name = very_long_variables_name

 


#2.5

Array

1. create

const thu : "thu";

const fri : "fri";

const sat : "sat";

const sun : "sun";

const dayOfWeek = ["mon", "tue", "wed", thu, fri, sat, sun]; //array

const nonsence = [true, 1, "hello", flase, null, undefine];

2. getItem

// Get Item from array

console.log(dayOfWeek[5] ); //"sat"

3. push

// Add one more day to the array

dayOfWeek.push("sun");

#2.6

Object

1. create

const player = {

 name : "nico",

 points : 10,

 fat : true

}

//object안의 규칙은 밖과 다르다

// = -> :

// ; -> ,

2. getItem

console.log(player.name)	//그냥 사용

console.log(player["name"])	//배열처럼 사용

 

3. adapt

player.name = "izuya"; 

//object인 player은 const라 변경이 불가능 하지만 그 안의 객체를 변경하는 것에는 문제가 없다.

//player자체를 변경하려 하면 문제가 발생한다

4. add

player.lastName = "potato" 

// lastName이란 속성은 없다, 따라서 새롭게 추가된다


#2.7, 2.8

Function

1. create

function sayHello(nameOfPerson){     

  console.log(nameOfPerson)

}



function sayHallong(nameOfPerson, age){   //두개도 가능

  console.log("Hello my name is " + nameOfPerson + " and I'm " + age);

}

2. run

sayHello("nico");    //argument(인수)값 입력

sayHello("izuya");



sayHallong("nico", 35);

sayHallong("izuya", 23);

 

Function in Object

1. create

const player = {

 name : "nico",

 sayHello : function(otherPersonsName){

  consol.log("hello!" + otherPersonsName + " nice to meet you);

 },

};

2. run

console.log(player.name);

player.sayHello("lynn");

#2.11, 2.12

Object Returns

const age = 96;

function calculateKrAge(ageOfForeigner){

 return ageOfForeigner + 2;

}



const krAge = calculateKrAge(age);



console.log(krAge);


#2.13 2.14 2.15

ParseInt

parseInt("15"); //숫자를 int로 변환해줌

console.log(typeof "15", typeof(parseInt("15");

TypeOf

const age = "1234"

console.log(typeof age); //변수 age의 타입을 봄

//typeof(age) - 이렇게도 될거임

isNaN

console.log(isNaN(age)); //age가 Nan(Not a number)인지 확인

 

Conditionals(조건문)

1. if, else, else if

if(isNaN(age)){

console.log("please write a number");

} else if(age < 18)

 console.log("Your are too young.")

} else{

 console.log("Thank you for writing your age.");

}

2. ===

if( age === 18){

 console.log("So hot");

}

// js는 == 대신 === 사용

3. 연산자

  • >
  • <
  • ===
  • >=
  • <=
  • &&  - AND
  • ||     - OR
  • !      - NOT