let result = 0;
for (let value of array) {
result += value;
}
console.log(result/array.length);
while
로 1부터 100까지 짝수만 더해보세요.let num = 0;
do {
console.log(num);
num += 1;
} while (num < 11);
let i = 1;
let sum = 0;
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}
console.log(sum);
while
문으로 변경하기for (var i = 2; i < 10; i++) {
for (var j = 1; j < 10; j++) {
document.write(`${i} X ${j} = ${i*j} <br>`);
}
}
let i = 2;
let j = 1;
while (i < 10) {
while (j < 10) {
document.write(`${i} X ${j} = ${i*j} <br>`);
j++;
}
j = 1; // 초기화가 필요하다
i++;
}
while
구구단 5단까지 출력let i = 2;
let j = 1;
while (i < 10) {
if (i == 6){
break;
}
while (j < 10) {
console.log(`${i} X ${j} = ${i*j}`);
j++;
}
i++;
j = 1;
}
while
구구단 X 5
까지 출력let i = 2;
let j = 1;
while (i < 10) {
while (j < 10) {
console.log(`${i} X ${j} = ${i*j}`);
if (j == 5){
break;
}
j++;
}
i++;
j = 1;
}
Array(배열)
String(문자열)
Number(숫자)
Boolean(논리값)
Object(객체)
undefine : undefind
null : object
NaN : number
for (let i = 0; i < 10; i++) {
console.log(i)
}
let a = [10, 20, 30, 40];
for (let i of a) {
console.log(i);
}
let a = [10, 20, 30, 40];
for (let i in a) {
console.log(i);
}
let x = 0;
while (x < 10) {
console.log(x);
x++;
}
let x = 0;
do {
console.log(x);
x++;
} while (x < 10)
let a = [10, 20, 30, 40];
a.forEach(e => console.log(e2));
for (let i = 0; i < 10; i++) {
if (i == 5) {
break;
}
}
for (let i = 0; i < 10; i++) {
if (i == 5) break;
console.log(i);
}
for (let i = 0; i < 10; i++) {
if (i == 5) break;
console.log(i);
}
for (let i = 0; i < 10; i++) {
if (i == 5) continue;
console.log(i);
}
function add(x, y){
return x + y;
}
add(3, 5)
- 콜백함수
function add(x, y) {
return x + y;
}
function mul(x, y) {
return x * y;
}
function cal(a, b){
return a(10, 10) + b(10, 10);
}
cal(add, mul);
- 화살표함수
function add(x, y) {
return x + y
}
let addArrow = (x, y) => x + y;
- 익명함수 아님
// 기명 함수
let aa = function sum(x, y) {
return x + y
}
// 익명 함수인것 같지만 바뀜
let bb = function(x, y) {
return x + y
}
// ES5에서는 빈 문자열이었는데 ES6에서 name 값을 가지는 것으로 바뀌었습니다.
let cc = (x, y) => x + y;
let x : 변수로 사용하세요.
var x (전역에서 사용할 변수값, 실무에서 되도록 사용X)
const x (변하지 않는 상수값)
function add(...x){
return x;
}
add(1, 2, 3, 4, 5)
hojun이 not defined 되었다고 나옵니다.
if (true){
let hojun = 100;
}
console.log(hojun);
// 리터럴
prompt('hello?')
'hi'
comfirm()
VM188:1 Uncaught ReferenceError: comfirm is not defined
at <anonymous>:1:1
(익명) @ VM188:1
confirm('hi')
true
alert('hello')
undefined
아좌좌~~