250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JavaScript
- useState
- typeScript
- 삶
- tailwindcss
- API
- hardhat
- error
- Ethereum
- Interface
- 기준
- CSS
- REACT
- CLASS
- graphQL
- HTML
- node.js
- Redux
- blockchain
- bitcoin
- middleware
- evm
- concept
- Props
- SSR
- solidity
- web
- built in object
- express.js
- nextJS
Archives
- Today
- Total
ReasonJun
Javascript : Loop 본문
728x90
while / do while
// while
let n = 0;
while (n < 4) {
console.log(n);
n += 1;
}
// 0
// 1
// 2
// 3
// do while
do {
console.log(n);
n += 1;
} while (n < 4);
// 0
// 1
// 2
// 3
// 4
switch
function price(fruit) {
switch (fruit) {
case 'apple':
return 1000;
case 'banana':
return 1500;
default:
return 0;
}
}
console.log(price('apple')); // 1000
console.log(price('banana')); // 1500
console.log(price('hello')); // 0
for
for (let i = 0; i < 10; i += 1) {
console.log(i);
}
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
for (let i = 0; i < 10; i += 1) {
if (i > 4) {
break;
}
console.log(i);
}
// 0
// 1
// 2
// 3
// 4
for (let i = 0; i < 10; i += 1) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
// 1
// 3
// 5
// 7
// 9
const fruits = ['apple', 'banana', 'cherry'];
for (const a of fruits) {
console.log(a);
}
// apple
// banana
// cherry
const user = {
name: 'you',
age: 85,
isValid: true,
email: 'wewe@gmail.com',
};
for (const key in user) {
console.log(key);
console.log(user[key]);
}
// name
// you
// age
// 85
// isValid
// true
// email
// wewe@gmail.com
// Since object data does not have order, so the output order may not match.
iterator
// [Symbol.iterator](): IterableIterator<T>;
// repeatable
const array = [1, 2, 3];
console.log(array.values()); // Object [Array Iterator] {}
console.log(array.entries()); // Object [Array Iterator] {}
console.log(array.keys()); // Object [Array Iterator] {}
// iterator
const iterator = array.values();
const test_iterator = array.values();
while (true) {
console.log(test_iterator.next());
const item = iterator.next();
if (item.done) break;
console.log(item.value);
/*
{ value: 1, done: false }
1
{ value: 2, done: false }
2
{ value: 3, done: false }
3
{ value: undefined, done: true }
*/
}
const multiple = {
[Symbol.iterator]: () => {
const max = 10;
let num = 0;
return {
next() {
return { value: num++ * 2, done: num > max };
},
};
},
};
for (const num of multiple) {
console.log(num);
}
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
function makeIterable(initialValue, maxValue, callback) {
return {
[Symbol.iterator]: () => {
const max = maxValue;
let num = initialValue;
return {
next() {
return { value: callback(num++), done: num > max };
},
};
},
};
}
const multiple = makeIterable(0, 20, (num) => num * 2);
for (const num of multiple) {
console.log(num);
}
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// 20
// 22
// 24
// 26
// 28
// 30
// 32
// 34
// 36
// 38
728x90
'Frontend > Javasciprt' 카테고리의 다른 글
Javascript : Operators Basic (0) | 2023.06.07 |
---|---|
javascript : spread / destructing assignment (0) | 2023.06.07 |
javascript : built-in object [array] (0) | 2023.06.07 |
Javascript : Function (2) | 2023.06.07 |
Javascript : class (0) | 2023.06.07 |
Comments