💎자바스크립트 기본 문법 정리
기본값 : .includes('', 0); // 숫자 0 은 검색을 시작할 위치
const str = 'hello world';
console.log(str.includes('hello',0)); // true
console.log(str.includes('hello',1)); // false
const array1 = [1, 2, 3];
console.log(array1.includes(2)); //true
console.log(array1.includes(4)); //false
기본값 : .indexOf()
const str = 'hello Python'
012345678901
console.log(str.indexOf('o')); // 4
console.log(str.indexOf(' ')); // 5
console.log(str.indexOf('z')); // -1
기본값 : .slice()
const str = 'hello world!!'
console.log(str.slice(0,5)) // 'hello'
console.log(str.slice(1,3)) // 'el'
console.log(str.slice() // 'hello world'
기본값 : .trim()
const str = ' hello '
console.log(str.trim()); // hello
기본값 : .padEnd(), .padStart()
const str = '12345'
console.log(str.padEnd(10, '0')); // 1234500000
console.log(str.padStart(10, '0')); // 0000012345
기본값 : .split()
const str = 'Apple,Banana,Cherry'
console.log(str.split(',')); // ['Apple','Banana','Cherry']
console.log(str.split(' ')); // ['Apple,Banana,Cherry']
기본값 : .toLocalString()
const num = 1000000
console.log(num.toLocalString()); // 1,000,000
기본값 : .parseInt()
const str = `3.141592`
const num = 3.141592
console.log(Number.parseInt(str, 10)); // 3
console.log(parseInt(str, 10)); // 3
console.log(parseInt(str)); // 3
기본값 : .isNaN()
const num1 = 123
const num2 = 'hello'
const num3 = null
const num4 = '123'
console.log(isNaN(num1)); // false
console.log(isNaN(num2)); // true
console.log(isNaN(num3)); // false
console.log(isNaN(num4)); // true
기본값 : Number.isInteger()
const num = 123
const pi = 3.14
const stringNum = '123'
console.log(Number.isInteger(num)); // true
console.log(Number.isInteger(pi)); // false
console.log(Number.isInteger(stringNum)); // false
기본값 : Math.max(), Math.min()
console.log(Math.max(1,2,3,4)); // 4
console.log(Math.min(1,2,3,4)); // 1
기본값 : Math.pow(), Math.abs()
console.log(Math.pow(4, 2)); // 16
console.log(Math.abs(-2)); // 2
기본값 : Math.round(), Math.floor(), Math.ceil(), Math.random()
const pi = 3.14
console.log(Math.round(pi)); // 3
console.log(Math.floor(pi)); // 3
console.log(Math.ceil(pi)); // 4
console.log(Math.random()); // 0~1
기본값 : .at()
const arr = [1,2,3,4]
console.log(arr[0]); // 1
console.log(arr.at(0)); // 1
기본값 : .concat()
const arr = [1,2,3]
const arr2 = [4,5,6]
const total = arr.concat(arr2);
console.log(total); // [1,2,3,4,5,6]
기본값 : .every(() => {})
const arr = [1,2,3,4]
const isValid = arr.every(x => x > 0 )
console.log(isValid); // true
기본값 : .some(() => {})
const arr = [1,2,3,4]
const isValid = arr.some(x => x > 3)
console.log(isValid); // true
기본값 : .filter(() => {})
const numbers = [1,2,3,4,5]
const filnum = numbers.filter(x => x > 3);
console.log(filnum) // [4, 5]
//주로 이렇게 사용됨
const user = [
{name: 'chan', age: 26},
{name: 'kim', age: 25},
{name: 'park', age: 24}
]
const superUser = user.filter(x => x.age > 24);
console.log(superUser) // [{name: 'chan', age: 26},{name: 'kim', age: 25}]
기본값 : .find(() => {})
const arr = [5,8,150,13,2]
const foundItem = arr.find(x => x > 4);
const foundItem1 = arr.find(x => x > 20);
console.log(foundItem); // 8
console.log(foundItem); // 150
기본값 : .findIndex(() => {})
const arr = [5,8,150,13,2]
const foundIndex = arr.findIndex(x => x > 140);
console.log(foundIndex); // 2
기본값 : flat()
const arr = [1,2,3, [4,5,[6,7,[8,9]]]]
console.log(arr.flat()); // [1,2,3,4,5,[6,7]]
console.log(arr.flat(2)); // [1,2,3,4,5,6,7,[8,9]]
console.log(arr.flat(Infinity)); // [1,2,3,4,5,6,7,8,9]
기본값 : forEach(() => {})
const arr = [1, 2, 3]
arr.forEach(x => x * 3);
console.log(arr); // [3, 6, 9]
기본값 : .includes()
const arr = [1, 2, 3]
console.log(arr.includes(2)); // true
//주의점 - 객체를 찾을때는 변수에 넣어서 찾아주자.
const users = [
{name: 'chan'},
{name: 'kim'}
]
console.log(users.includes({name:'chan'})); // false
const a = users[0]
console.log(users.includes(a)); // true
기본값 : .join()
const arr = ['Apple', 'Banana', 'Cherry']
console.log(arr.join()); // 'Apple,Banana,Cherry'
console.log(arr.join(',')); // 'Apple,Banana,Cherry'
console.log(arr.join('.')); // 'Apple.Banana.Cherry'
console.log(arr.join('/ ')); // 'Apple/ Banana/ Cherry'
기본값 : .map(() => {})
const numbers = [1,2,3,4]
const newNumbers = numbers.map(x => x * 2);
// 객체 사용
const users = [
{name: 'chan', age: 26},
{name: 'kim', age: 25},
{name: 'park', age: 24}
]
const newUsers = users.map(user => ({
...users,
isValid: true,
email: null
}));
console.log(newUsers); // [
// {name: 'chan', age: 26, isValid: true, email:null},
// {name: 'kim', age: 25, isValid: true, email:null},
// {name: 'park', age: 24, isValid: true, email:null}
// ]
기본값 : .pop(), .push(), .shift(), .unshift()
const fruits = ['Apple', 'Banana', 'Cherry']
console.log(fruits.pop()); // ['Apple', 'Banana']
console.log(fruits.push('Cherry')); // ['Apple', 'Banana', 'Cherry']
console.log(fruits.shift()) ; // ['Banana', 'Cherry']
console.log(fruits.unshift('Apple')); // ['Apple', 'Banana', 'Cherry']
기본값 : .reduce((acc, cur) => {}, 0)
const numbers = [1, 2, 3]
const sum = numbers.reduce((acc, cur) => {
return acc + cur
}, 0);
console.log(sum); // 6
//객체 사용
const users = [
{name: 'chan', age: 26},
{name: 'kim', age: 25}
]
const arrSum = users.reduce((acc, cur) => {
acc.push(cur.name)
return acc
},[]);
console.log(arrSum); // [{name: 'chan'},{name: 'kim'}]
기본값 : .reverse()
const num = [3,4,1,2]
console.log(num.reverse()); // [2,1,4,3]
console.log(num.sort((a, b) => a - b)); // [1,2,3,4]
console.log(num.sort((a, b) => b - a)); // [4,3,2,1]
기본값 : .slice( , )
const arr = ['a', 'b', 'c', 'd', 'e']
console.log(arr.slice(0,2)); //['a', 'b']
console.log(arr.slice(1,4)); //['b', 'c', 'd']
console.log(arr.slice()); //['a', 'b', 'c', 'd', 'e']
기본값 : .splice
const arr = ['a', 'b', 'c']
arr.splice(2,0, 'A');
console.log(arr); // ['a', 'b', 'A', 'c']
arr.splice(1,3, 'B', 'C', 'D');
console.log(arr); // ['a', 'B', 'C', 'D']
const arraylike = {
0: 'A',
1: 'B',
2: 'C',
length: 3
}
// .constructor -> 어떤 prototype 으로 만들어졌는지
console.log(arraylike.constructor === Array); // false
console.log(arraylike.constructor === Object); // true
Array.from(arraylike); // ['A', 'B', 'C']
console.log(arraylike.constructor === Array); // true
기본값 : Object.assign()
const target = {a: 1, b: 2}
const source = {a: 1, c: 3}
const source1 = {d: 4, e: 5}
const result = Object.assign(target, source, source1);
console.log(result); // {a: 1, b: 2}
console.log(target); // {a: 1, b: 2, c: 3, d: 4, e: 5}
기본값 : Object.entries()
const user = {
name: 'Chan',
age: 26,
isValid: true
}
console.log(Object.entries(user));
// [Array(2), Array(2), Array(2),]
// [['name', 'Chan'], ['age', '26'], ['isValid', 'true']]
const user = {
name: 'chan',
age: 26,
isValid: true
}
console.log(Object.keys(user)); // ['name', 'age', 'isValid']
const user = {
name: 'chan',
age: 26,
isValid: true
}
console.log(Object.values(user)); // ['chan', 26, true]