//
, 여러줄 주석 /*...*/
let address; //선언
address = "선릉"; //할당
//함수를 정의한다
function checkCorrect() {
let hi = "안녕하세요";
return hi;
}
//함수를 호출한다
checkCorrect();
undefined
라는 값을 반환한다.///name = 매개변수 parameter
function getName(name) {
return name + '님';
}
// '개발자' = 인자 argument
getName('개발자')
//let newNum = num++
let num = 1; // num = 1
let newNum = num;
num++; // num = 2, newNum = 1
//let newNum = ++num
let num = 1; // num = 1
num++; // num = 2
let newNum = num; // newNum, num = 2
+
로 연결 가능하다let message = "감사합니다. ";
let userName = "김개발";
let banger = "님!";
let customMess = message + userName + banger;
console.log(customMess); //''감사합니다. 김개발님!''
2 + 2 //4
'2'+ 2 //'22'
'2'+'2'//'22'
==
,===
,!=
,!==
>
,<
,>=
,<=
===
값과 타입이 같은지를 비교하는 동등 비교 연산자 (equality operator), 엄격한 비교연산자!==
같지 않음==
느슨한 비교연산자===
를 쓰자||
&&
\\
나 &&
를 쓸때 괄호로 어디까지 묶어주는지가 중요하다 **function alertSuccess() {
alert("로그인 성공!");
}
alertSuccess()
-인자값을 전달 하는 경우
function alertSuccess(name) {
alert(name + "님 로그인 성공!");
}
alertSuccess("김개발") // "김개발님 로그인 성공!"
function meetAt(year, month, date){
if (year && month && date){ //
return year+"/"+month+"/"+date
}
if (year && month) {
return year+"년 "+month+"월"
}
if (year){
return year+"년"
}
return
}
//year+"년" 먼저 할 경우 월과 일이 있어도 year이 true로 early return 되어버림 **
meetAt(2021) //2021년
meetAt(2021,2) //2021년 2월
meetAt(2021,2,11) //2021/2/11
for (반복조건) {
//반복조건이 맞으면 실행할 코드
}
array.push()
: array의 마지막부분에 요소를 추가array.unshift()
: array의 맨 앞부분에 요소를 추가array.pop()
: array의 마지막부분 요소를 제거array.shift()
: array의 맨 앞부분에 요소를 제거typeof 42 //number
typeof 'word' //string
typeof true //boolean
typeof undefined //undefined
typeof null //object
typeof [] //object
null
의 데이터타입은 object
; null
은 빈 객체를 참조하고 있다array
의 데이터타입은 object
; 배열은 확장된 객체![] //false >> 빈배열은 true
!{} //false >> 빈객체는 true
!0 //true >> 0은 false
!100 //false >> 100은 true
!'' //true >> 빈스트링은 false
!'hi' //false >> 스트링은 true
!NaN //true >> NaN는 false
!undefined //true >> undefined는 false
"Word" !== "word"
let sayHi = "Hello, World!"
sayHi.indexOf("World") //7
let string = "305"
parseInt(string) // 305 Number!
Date() // 'Thu Feb 11 2021 18:33:59 GMT+0900 (Korean Standard Time)'
rightNow = new Date() // 'Thu Feb 11 2021 18:33:59 GMT+0900 (Korean Standard Time)'
year = rightNow.getFullYear(); // 2021
month = rightNow.getMonth()+1; // 2
date = rightNow.getDate(); // 11
day = rightNow.getDay(); // 4 (Thurs)
currentHour = rightNow.getHours(); // 18
currentMin = rightNow.getMinutes(); // 33
time = rightNow.getTime() // 1613036562629
class Car {
constructor(name, price) {
this.name = name;
this.price = price;
}
}
let methodObj = {
do: function() {
console.log('메서드 정의는 이렇게');
}
}
//ES5
const getName = function(name) {}
//ES6
const getName = (name) => {}
const getName = name => {}
//ES6
const hi = name => { return name };
const hi = name => name; // {} 생략 가능!
const name = `김개발`;
const name = '김개발';
const hi = `안녕하세요. 저는 ${name} 입니다.`;
//ES5 문법
const hi = '안녕하세요. 저는 ' + name + ' 입니다.';
const email = 'developer_front@gmail.com';
console.log(email.startsWith('dev')); // true
console.log(email.endsWith('com')); // true
console.log(email.includes('@gmail')); // true
'hello'.repeat(5) //'hellohellohellohellohello'
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
//위와 같음
const squares = arr.map(function (x) {
return x * x;
});
//////example 1
let startWithNames = [];
let names = ['a', 'ab', 'cbb', 'ada'];
names.forEach(el => {
if (el.startsWith('a')) {
startWithNames.push(el);
}
});
startWithNames // [ 'a', 'ab', 'ada' ]
//////example 2
let hasC = false;
let arr = ['a', 'b', 'c', 'd'];
arr.forEach(el => {
if (el === 'c') {
hasC = true;
return;
}
});
hasC // true
//////example 3
let idxOfC = -1;
let arr = ['a', 'b', 'c', 'd'];
arr.forEach((el, idx) => {
if (el === 'c') {
idxOfC = idx;
return;
}
});
idxOfC // 2
const information = {
name: '김개발'
}
const verb = 'developes' // [A]
const project = 'facebook' // [B]
information[verb] = project // [A]
information.developes = 'facebook' // [B]
information // { name: '김개발', developes: 'facebook' }
const obj = {
name: 'melon',
weight: 4350,
price: 16500,
isFresh: true
}
Object.keys(obj) //[ 'name', 'weight', 'price', 'isFresh' ]
Object.values(obj) // [ 'melon', 4350, 16500, true ]
Object.entries(obj)
//[[ 'name', 'melon' ],[ 'weight', 4350 ],[ 'price', 16500 ],[ 'isFresh', true ]]
for (let i = 0; i < arr.length; i ++) //for문
for (let i in arr) //for-in
const obj = {
name: 'melon',
weight: 4350,
price: 16500,
isFresh: true
}
for (let key in obj) {
const value = obj[key]
}
//index.html
<script src="index.js"></script>
<script>
태그가 있다면 Javascript 코드를 작성할수 있다.요소.addEventListener(이벤트종류, function() {
//이벤트가 일어났을 때 실행할 내용
});