프로그래밍 기본 함수

a % b a를 b로 나눈 나머지가 나온다
a < b a보다 b가 크다
a > b a가 b보다 크다
a == b a와 b는 같다
a != b a와 b는 같지 않다
a && b a가 b이고 (and)
a || b a 거나 b 이다 (or)

 

 

1. 변수

let (변수) = ?

: 박스이다.

변수는 값을 담는 박스다.

문자열도 담을 수 있다. ( ' ' 사용해야함)

예시) let a = 2 / let first_name = Tite

 

 

 


2. 자료형

let a_list = [ ]

: 리스트 자료형은 순서가 중요한 담기

[ ] 대괄호 사용

컴퓨터는 1부터 세지 않고, 0부터 세기 때문에 a_list의 '수박'은 0번째이다.

뒤늦게 리스트를 추가하고 싶을 때는 a_list.push(' ')

let a_list = ['수박', '참외', '배']
a_list[0]
"수박"
a_list.push('감')
4
a_list
(4) ['수박', '참외', '배', '감']
a_list[3]
"감"

 

let a_dict = {  :  ,  :  }

순서는 중요하지 않다.

쓸 때는 { } 괄호,  가져올 때는 [ ] 괄호를 사용한다.

추가하고 싶을 때는 a_dict[  ] = ?

let a_dict = {'name':'bob','age':27}
a_dict['name']
'bob'
a_dict['height'] = 180
180
a_dict
{'name':'bob','age':27, 'height':180}

 

list와 dict 는 서로 서로의 요소로 쓰일 수 있다.

a_dict
{name:'bob', age:27, height: 180}
a_dict['fruits'] = a_list
(4) ["수박", "참외", "배", "감"]
a_dict
{name:'bob', age:27, height: 180, fruits: Array(4)}
a_dict['fruits'][2]
"배"

 

글씨를 분리하는 방법

let myemail = 'miseria_98@naver.com'
undefined
myemail.split('@')
(2) ['miseria_98', 'naver.com']
myemail.split('@')[1]
'naver.com'
myemail.split('@')[1].split('.')
(2) ['naver', 'com']
myemail.split('@')[1].split('.')[0]
'naver'

 

 

 


3. 함수

: 프로그래밍에서 함수란 정해진 동작을 하는 것.

function sum(num1, num2){
    return num1+num2
}
undefined
let result = sum(2,3)
undefined
result
5

num1, num2라는 변수를 받아서 그걸 더한 값을 return 해라.

num1(2) + num2(3)이 된다

num1(2) + num2(3) 을 더해 retrun은 5가 된다

(함수에서 retrun이란 끝내고 나를 변신시켜줘)

let result = sum(2,3)는 5가 되었기 때문에

result가 5가 찍히게 된다.

 

function mysum(num1, num2){
    alert('안녕!')
    return num1+num2
}
undefined
let result2 = mysum(2,3)
undefined
result2
5

2하고 3이 (num1, num2)에 들어가면

alert으로 '안녕!'을 띄워주라는 것 => 안녕을 띄웠으면

return num1(2)+ num2(3)을 더해서 5로 만들어 주고

return은 5로 변신한다.

result2에는 5가 들어가게 된다.

 

 


 

4. 조건문

let age = 24
undefined
if (age > 20 ) {
    console.log('성인입니다')
} else {
    console.log('청소년입니다')
}
성인입니다
undefined
let sex = '남성'
undefined
if (age > 20 && sex == '남성') {
    console.log('성인입니다')
} else {
    console.log('청소년입니다')
}
성인 남성입니다
undefined

 

 

 


5. 반복문

 

'JavaScript' 카테고리의 다른 글

[22.01.25] async 와 defer의 차이  (0) 2022.01.25

+ Recent posts