본문 바로가기

Programing/javascript

[javascript] 기본 문법

HTML 변경 하는 법
<p id="hello">바나나</p>

<script>
    document.getElementById('hello').innerHTML ='바나나우유'
</script>

 

클릭 이벤트 리스너
<p id="hello">바나나</p>
<button id="button">버튼</button>

<script>
    document.getElementById('button').addEventListener('click', function(){
        document.getElementById('hello').innerHTML ='바나나우유'
    })
</script>

 

변수 (자료저장)
let name = '딸기';
const Count = 10;

let 업데이트 가능, 재선언 불가능

const 업데이트, 재선언 불가능

 

함수(parameter)
<p id="hello">바나나</p>
<button id="button">버튼</button>

<script>
    document.getElementById('button').addEventListener('click', () => {
        changeName('바나나우유')
    })
    
    function changeName(parameter) {
    	document.getElementById('hello').innerHTML = parameter
    }
</script>

 

array, object
let name = ['바나나우유', '딸기우유'];
let fruit = {name : '바나나우유', count: 10};

 

조건부
//if else

if (true) {
	console.log('true');
} else {
	console.log('false')
}

// for

for (var i=0; i<3; i++) {
	cosnole.log('log');
}

// forEach 배열길이만큼 반복
[1, 2, 3, 4].forEach(function() {

});

*

&& (and) - 둘다 true

||(or) - 둘중 하나라도 true