JavaScript์ ํ์ ๋ฌธ์ (๋ฌธ์์ด ์นํ)
์ถ๋ ฅ๋ ๋ฌธ์์ด์ ํฌํจํ์ฌ ๋ณ์๋ก ์นํํ ์ ์๋ ํน์ ๋ฌธ์์ด๋ค.
log()์ฒ๋ผ ๋ฌธ์์ด์ ๋ฐ๋ console Method์ ์ฌ์ฉํ ์ ์๋ค. ์ค๋ณต ์ฌ์ฉ์ด ๋ถ๊ฐํ๋ฉฐ, ์ง์ ๋ง์ถฐ ์ฌ์ฉํด์ผ ํ๋ค.
ํ์ ๋ฌธ์ | ์ค๋ช |
%d or %i | ๋ชจ๋ ์ข ๋ฅ์ ์ซ์, ๋ ผ๋ฆฌ๊ฐ |
%s | ๋ฌธ์์ด ์ฌ์ค์ ๋ง๋ฅ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ |
%o | ๊ฐ์ฒด false๋ 0, true๋ 0 ์ด์ธ์ ๋ชจ๋ ์ (๋ณดํต 1) |
%j | JSON |
%f | ๋ถ๋ ์์์ ์ |
* ๋ถ๋ ์์์ ์ ์ฐธ๊ณ : http://www.tcpschool.com/cpp/cpp_datatype_floatingPointNumber
1. %d = ์ซ์ ๋ฐ ๋ ผ๋ฆฌ ๊ฐ
/**๋ฐ์ดํฐ ํ์
*/
const num = 123;
const str = "hello";
const bool = true;
const obj = new Date();
const arr = [1,2,3];
const json = {"a":123, "b":234};
/** %d ์ซ์ ๋ฐ ๋
ผ๋ฆฌ๊ฐ*/
console.group("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ");
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", num); // 123
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", str); // NaN
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", true); // 1
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", obj); //๊ฐ์ฒด์ ๋ํ Hash๊ฐ ์ถ๋ ฅ // 1650531201214
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", arr); // NaN
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%d", json); // NaN
console.groupEnd();
2. %s = ๋ฌธ์์ด
/**๋ฐ์ดํฐ ํ์
*/
const num = 123;
const str = "hello";
const bool = true;
const obj = new Date();
const arr = [1,2,3];
const json = {"a":123, "b":234};
/** %s ๋ฌธ์์ด */
console.group("๋ฌธ์์ด ์ถ๋ ฅํ๊ธฐ");
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", num); // 123
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", str); // hello
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", true); // true
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", obj); // 2022-04-21T08:54:40.300Z
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", arr); // [ 1, 2, 3 ]
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%s", json); // { a: 123, b: 234 }
console.groupEnd();
3. %o ๊ฐ์ฒด
/**๋ฐ์ดํฐ ํ์
*/
const num = 123;
const str = "hello";
const bool = true;
const obj = new Date();
const arr = [1,2,3];
const json = {"a":123, "b":234};
/** %o ๊ฐ์ฒด */
console.group("๊ฐ์ฒด ์ถ๋ ฅํ๊ธฐ");
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", num); // 123
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", str); // 'hello'
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", true); // true
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", obj); // 2022-04-21T08:54:40.300Z
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", arr); // [ 1, 2, 3, [length]: 3 ]
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%o", json); // { a: 123, b: 234 }
console.groupEnd();
4. %j = JSON
/**๋ฐ์ดํฐ ํ์
*/
const num = 123;
const str = "hello";
const bool = true;
const obj = new Date();
const arr = [1,2,3];
const json = {"a":123, "b":234};
/** %j JSON */
console.group("JSON ์ถ๋ ฅํ๊ธฐ");
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", num); // 123
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", str); // "hello"
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", true); // true
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", obj); // "2022-04-21T08:54:40.300Z"
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", arr); // [1,2,3]
console.log("์ซ์๊ฐ ์ถ๋ ฅํ๊ธฐ=%j", json); // {"a":123,"b":234}
console.groupEnd();
๋ณตํฉ ์ฌ์ฉ
ํด๋นํ๋ ๋ฐ์ดํฐ ํ์ ๊ณผ ๋ง๋ ํ์ ๋ฌธ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
const name = "ํ๊ธธ๋";
const age = "20";
console.log("%s๋์ ๋์ด๋ %d์ธ์
๋๋ค.", name, age);
// ํ๊ธธ๋๋์ ๋์ด๋ 20์ธ์
๋๋ค.
'Study > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] ์ฐ์ฐ์(operator) (0) | 2022.04.23 |
---|---|
[JavaScript] ์ด์ค์ผ์ดํ ์ํ์ค (์ด์ค์ผ์ดํ ๋ฌธ์) (0) | 2022.04.21 |
[JavaScript] ๋ฐ์ดํฐ ํ์ (0) | 2022.04.21 |
[JavaScript] ๋ณ์์ ์์ (0) | 2022.04.21 |
[JavaScript] JavaScript๋? (0) | 2022.04.14 |