JavaScript
[자바스크립트] 정리 1
재키재키
2022. 1. 15. 22:08
다 까먹은 javascript, 노마드코더 javascript 강의를 들으면서 내용정리..
변수명 관례
camelCase: javascript ex) myName
snake_case: python ex) my_name
types
always const(상수) sometimes let(변수) never var
boolean: true, false
null: 비어있음을 명시하는것(값이 존재) *파이썬에서는 None
undefined: 메모리만 할당(값을 아직 부여하지 않음) *파이썬에는 존재하지 않음
Nan: 낫 어 넘버
const amIFat = null;
let something;
console.log(amIFat, something); // null , undefinded (type)
larray -> [ ] 사용, 서로 다른 타입의 원소 ㅆ가능, index로 접근, 수정가능
push, pop 등 제공
const toBuy = ["potoato", "cheese", "pizza"];
console.log(toBuy);
// ['potoato', 'cheese', 'pizza']
toBuy.push("melon");
console.log(toBuy);
// ['potoato', 'cheese', 'pizza', 'melon']
object 만들기
사이에 , 를 반드시 써줘야 한다.
이걸 json이라고 했던거 같은데...
const인 object는 자체는 수정 불가능하지만 안의 내용물을 수정이 가능하다.
const player = {
name: "joon",
points : 20,
fat: false,
};
console.log(player); //{name: 'joon', points: 20, fat: false}
player.points += 10;
player.lastName = "Park"
console.log(player); //{name: 'joon', points: 30, fat: false, lastName: 'Park'}
function 과 method 만들기
function에 argument를 여러개 줄수 있다. 근데 만약 초과되는 argument를 전달하면 앞에 필요한 개수까지만 전달된다.
function은 아무런 value를 retrun을 하지 않을 수도 있다.
function sayHello(person){
console.log("Hello " + person);
}
const player = {
name : "joon",
sayHello : function (otherPerson) {
console.log("hello " + otherPerson + " I'm " +player.name );
},
}
sayHello("Jisoo");
player.sayHello("Youseong");
function plus(a, b){
return a + b;
}
const result = plus(1,2); //console에서 result라는 변수에 접근이 이제 가능하다!
조건문
if (condition) { }
else{ }
const age = parseInt(prompt("How old are you?"));
if (isNaN(age) || age < 0){ // is a not a number?
console.log("Please write a real positive number");
} else if (age < 18) {
console.log("Yor are too young");
} else if (age >= 19 && age<= 50) {
console.log("Yor can drink");
} else {
console.log("Umm not recommend");
}
== vs ===
== 동등 비교(type 호환가능)
=== 완전 동등 비교