function getTax(price) { return price * 0.1; //price에 tax를 곱한 값 } function calculateTotal(price) { return price + getTax(price); //price에 tax를 더한 값★ } const result = calculateTotal(3000); console.log(result); //3,300 function getTotal(price1, price2) { return calculateTotal(price1) + calculateTotal(price2); //★ } console.log (getTotal(1000, 4000)) //5,500
함수엔 input과 output이 중요하다. 하나의 함수에 한가지 일만 하도록 작성한다. 함수를 무언가를 동작하는 것이기 때문에 동사형태로 이름을 지정해야 한다. 자바스크립트에서의 함수는 타입이 없기 때문에 숫자가 문자열로 변환되어 출력된다. 다른 함수에서 타입이 중요한 경우 자바스크립트가 난해할 수 있다. 이것을 보완하고자 typescript가 나왔다. typescript는 명확하고 개발을 더 쉽게 만들어준다. 데이터 반환하기| function addTen(num) { return num + 10; } const result = addTen(5); console.log(result) //15 텍스트 문자열의 연결| function textConcatenation() { const text = "1 더하..
반복문에서 배열의 순서가 중요했다면 객체는 순서가 없기 때문에 index number를 기억하지 않고 key 값에 접근할 수 있다. 객체에 접근하는 방법 · Object Literal : 중괄호 안에 property(객체 내부의 값)와 method(객체 내부의 함수) 나열 · Dot Notation : 점 표기법으로 '.'(점)을 이용 · Bracket Notation : 대괄호를 이용하여 객체에 접근 Object Literal function iAmGround() { const myself = { name: 'JN', bloodType: 'A형', mbti: 'INTJ', favoriteCoffee: '아이스블론드바닐라더블샷마끼아또' } return myself; } console.log(iAmGrou..
반복문 반복문은 무한으로 코드를 반복해준다. * for문의 구조 for(초기화; 조건문; 증감식) { //반복 구문 } * 조건문이 true면 {반복 구문}이 실행된다 → 증감식 → 조건문 true → 반복조건이 실행 ex. 0~5까지 반복 function forLoops() { const array = []; for(let i = 0; i = 0; i--) {reverse.push(arr[i]) console.log (reverse) } /* 결과 [9] [9, 6] [9, 6, 4] [9, 6, 4, 3] [9, 6, 4, 3, 1] */ {}에서 console.log의 위치가 밖에 있냐, 안에 있냐에 따라 값은 달라진다. ex. 배열의 합계 function arrayLoop() { const myA..
배열(Array) : 순서대로 여러개의 데이터를 저장한다. index|배열된 요소에 접근, 확인, 수정 다차원배열(multi-dimensional array)_1 function multiArray() { const myArray = [["becoming", 29], ["developers", 30]]; return myArray; } console.log (multiArray()) 다차원배열(multi-dimensional array)_2_문자 array에 접근 function getElement() { const arr = [3, [5, ["array", 8], 2 + 3], [0]]; return arr[1][1][0]; } console.log (getElement()) //array arr[0]..
if 조건문에 비교 연산자를 사용할 수 있다. 연산자 종류 종류 기호 문법 의미 비교 연산자 == A==B A와 B가 같은가? != A!=B A와 B가 다른가? === A===B A와 B가 일치하는가? (type까지 고려) !== A!==B A와 B가 일치하지 않는가? >, 3, A=, =3, A= 21) { return "Nice to meet you, " + name + "!" } } const output1 = checkAge('Min', 21); const output2 = checkAge('Seok', 17); console.log(output1); //Nice to meet you, Min! ex3. string 문자열이 동일하면 true, 동일하지 않으면(else) false를 반환한다. f..
함수(function) 함수는 작업을 수행하기 위한 코드 블럭으로 자바스크립트의 객체(함수와 변수의 연관된 것들을 그룹화하는 도구)이다. 함수로 두 개 숫자의 합 구하기 const a = 12345; const b = 67890; function sum() { console.log(a + b) } sum() //80,235 함수로 문자열의 갯수 구하기 const word = "JUST DO IT" function getStringLength() { console.log(word.length) }; getStringLength() //10
자바스크립트로 문자열 길이를 구할 수 있다. ex.1 const str = "EEE"; console.log(`${str.length}`); 결과값 : 3 ex.2 const word1 = 'green' const word2 = 'red' const word3 = 'black' const length1 = word1.length; const length2 = word2.length; const length3 = word3.length; console.log(length1 + length2 + length3) 결과값 : 13
- Total
- Today
- Yesterday