TIL - Method 정리

홍예찬·2020년 9월 5일
0
post-thumbnail

Number Method

parseFloat()

  • 소수점 자리까지 반환.

parseInt()

  • 소수점 자리를 제외하고 정수까지만 반환.

Number()

  • Number type으로 바꾸는 가장 쉬운 방법이지만 문자형 숫자가 아닌 다른 문자가 들어갈 경우 NaN이 뜸

.toString()

  • Number type -> String type

또한 연산의 성질을 이용해서 바꿀 수 있다.

var numberAsNumber = 1234; 
var numberAsString = 1234 + ""; // Number + String = String 성질 이용

var numberAsNumber = "1234"; 
var numberAsString = numberAsNumber - 0; // 산술연산자 특징을 통해 Number type으로 변경

Math Method

Math.round()

  • 반올림
let num = 23.635356;
let round = Math.round(num);
console.log(round);
//output: 24

Math.ceil()

  • 올림
let num = 23.635356;
let ceil = Math.ceil(num);
console.log(ceil);
//output: 24

Math.floor()

  • 내림
let num = 23.635356;
let floor = Math.floor(num);
console.log(floor);
//output: 23

Math.random()

  • 0.0000000000000000에서 0.9999999999999999 사이 값에서 랜덤으로 값 return
let random = Math.random();
console.log(random);
//output: 0.05774420102327715

Math.abs()

  • 주어진 숫자의 절대값을 반환
function difference(a, b) {
  return Math.abs(a - b);
}

console.log(difference(3, 5));
// expected output: 2

console.log(difference(5, 3));
// expected output: 2

Array Method

.pop()

  • 배열에서 마지막 요소를 제거하고 그 요소를 반환
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
plants.pop()
console.log(plants);
// output: ['broccoli', 'cauliflower', 'cabbage', 'kale']
console.log(plants.pop())
// output: tomato

.push()

배열의 끝에 새로운 요소를 추가하고 배열의 새로운 길이를 반환

const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

.shift()

배열의 처음 요소를 제거하고 제거된 요소를 반환

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
plants.shift()
console.log(plants);
//output: [ 'cauliflower', 'cabbage', 'kale', 'tomato' ]
console.log(plants.shift())
//output: cauliflower

.unshift()

배열의 앞에 새로운 요소를 추가하고 배열의 새로운 길이를 반환

const array1 = [1, 2, 3];
let plusArray = array1.unshift(4, 5)
console.log(plusArray);
// output: 5
console.log(array1);
// output: Array [4, 5, 1, 2, 3]

.slice()

  • .slice() 는 어떤 배열을 자를 때 사용. 배열 내에 띄어쓰기까지 인식을 한다.
  • .slice(a, b) 에서 a는 처음 잘릴 시작위치, b는 잘릴 끝위치를 의미한다.
  • 그러나 b는 실질적으로 잘리지 않고 그 앞까지만 자른다.
let arr = [ 1, 2, 3, 4, 5, 6, 7 ];
let newArr = arr.slice( 3, 6 );
console.log(newArr); 
//output: [ 4, 5, 6 ]

Index에 직접 접근해서 Array 조작.

let day = ['m', 's', 'w', 't'];
day[1] = 't';
day[4] = 'f';
day[5] = 's'; 

//[ 'm', 't', 'w', 't', 'f', 's' ]

String Method

string 내에 있는 특정 string을 찾을 수 있는 Method.

includes()

startsWith()

endsWith()

const email = 'yealee.kim87@gmail.com';

console.log(email.startsWith('ye'));		//true
console.log(email.endsWith('com'));			//true
console.log(email.includes('@gmail'));		//true

String, Array 모두 사용하는 Method.

.indexOf()

  • 1)배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환하거나
    2)호출한 String 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환한다.
  • 해당 문자열이 없을 경우 -1을 반환한다.
String
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// output: "The index of the 2nd "dog" is 52"
Array
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
// output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

계속 추가 예정

profile
내실 있는 프론트엔드 개발자가 되기 위해 오늘도 최선을 다하고 있습니다.

0개의 댓글