IT EDU/JAVA SCRIPT

[JavaScript] JavaScript ๊ธฐ์ดˆ

yoonhoou 2022. 3. 21.
728x90

 

 

 

๐Ÿ”น๋ณ€์ˆ˜ ์„ ์–ธ

var type ์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ๋ณ€์ˆ˜์— ์•Œ๋งž๋Š” ๊ฐ’์„ ํ• ๋‹น.
var ๊ฐ€ ํ™•์žฅ์ด ๋˜์–ด let์œผ๋กœ ๋ณ€ํ•˜๊ณ  ์žˆ์Œ.
const(์ƒ์ˆ˜) : ์ดˆ๊ธฐํ™”๋ฅผ ์ œ์™ธํ•˜๊ณ  ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์—†๋Š” ๊ฐ’.
var a = 10; 
console.log(a);

let b = 20;
console.log(b);

const c = 30;
console.log(c);
10
20
30

 

 

๐Ÿ”นtypeof

typeof() : ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜.

let a = 10;
let b = 'Tiger';
let c = true;
let d = 3.14;
let e = []; // object type
let f = {}; // object type
let g = function(){} // function
let h = undefined; // ์ •์˜๋˜์ง€ ์•Š์€ ํƒ€์ž…

// number, string, boolean, object, function
console.log('a : ' + typeof(a));
console.log('b : ' + typeof(b));
console.log('c : ' + typeof(c));
console.log('d : ' + typeof(d));
console.log('e : ' + typeof(e));
console.log('f : ' + typeof(f));
console.log('g : ' + typeof(g));
console.log('h : ' + typeof(h));
a : number
b : string
c : boolean
d : number
e : object
f : object
g : function
h : undefined

 

 

๐Ÿ”น{ } - Key : Value

JSON - ์ด์ข… ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ํฌ๋งท ํ˜•์‹

JSON์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ์ง๋ ฌํ™”(Serializable) ํ•œ๋‹ค๊ณ  ํ‘œํ˜„.

 

  • JSON์€ ํ…์ŠคํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ, ์‚ฌ๋žŒ๊ณผ ๊ธฐ๊ณ„ ๋ชจ๋‘ ์ฝ๊ณ  ์“ฐ๊ธฐ ์‰ฝ๋‹ค.
  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ํ”Œ๋žซํผ์— ๋…๋ฆฝ์ ์ด๋ฏ€๋กœ, ์„œ๋กœ ๋‹ค๋ฅธ ์‹œ์Šคํ…œ ๊ฐ„์— ๊ฐ์ฒด๋ฅผ ๊ตํ™˜ํ•˜๊ธฐ์— ์ข‹๋‹ค.

 

https://ko.wikipedia.org/wiki/JSON

 

JSON - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

JSON(์ œ์ด์Šจ[1], JavaScript Object Notation)์€ ์†์„ฑ-๊ฐ’ ์Œ( attribute–value pairs and array data types (or any other serializable value)) ๋˜๋Š” "ํ‚ค-๊ฐ’ ์Œ"์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ๊ฐ„์ด ์ฝ์„ ์ˆ˜ ์žˆ

ko.wikipedia.org

 

 

1๏ธโƒฃ java ์ง๋ ฌํ™”

1. Serializable ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด๋ถ€๋Š” ๋น„์–ด์žˆ์ง€๋งŒ JVM์ด ์•Œ์•„์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•œ๋‹ค.
JSON์œผ๋กœ ๋Œ€์ถฉ ๋งŒ๋“ค๊ณ (์ง๋ ฌํ™”) ๋Œ€์ถฉ ๊ฐ€์ ธ์˜จ๋‹ค(์—ญ์ง๋ ฌํ™”)

2. FileOutputStream(byte์ €์žฅ) >> FileWriter(๋ฌธ์ž ์ €์žฅ)

3. java ์ง๋ ฌํ™”(java๋ผ๋ฆฌ๋งŒ), JSON ์ง๋ ฌํ™”(์–ธ์–ด์ƒ๊ด€ X)
FileOutputStream์€ Serializable์„ ์ƒ์†ํ•˜์ง€ ์•Š์œผ๋ฉด Exception ๋ฐœ์ƒ

// java ์ง๋ ฌํ™”
class Tiger implements Serializable{
	String name = "ํ˜ธ๋ž‘์ด";
	int age = 1000;
}

public class Hello {

	public static void main(String[] args) {
		Tiger tiger = new Tiger();
		
		try {
			// save
			FileOutputStream fos = new FileOutputStream("sample.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(tiger);
			fos.close();
			oos.close();
			
			// load
			FileInputStream fis = new FileInputStream("sample.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			Tiger t2 = (Tiger)ois.readObject();
			System.out.println(t2.name + " " + t2.age);
			fis.close();
			ois.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
ํ˜ธ๋ž‘์ด 1000

 

2๏ธโƒฃ JSON ์ง๋ ฌํ™”

// key : value -> JSON(์ด์ข…๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ํฌ๋งท ํ˜•์‹)
let obj = {
    a:10,
    b:'tiger',
    c:true,
    d:[],
    e:{},
    f:function(){},
    g:undefined
}

console.log(typeof(obj));
console.log(typeof(obj.a));
console.log(typeof(obj.b));
console.log(typeof(obj.c));
console.log(typeof(obj.d));
console.log(typeof(obj.e));
console.log(typeof(obj.f));
console.log(typeof(obj.g));
object
number
string
boolean
2 object
function
undefined

 

JSON ๊ฐ„๋‹จ ์˜ˆ์ œ

let obj = {
    a:{
        b:{
            c:{
                d:10
            }
        }
    },
}

let obj2 = {
    a:1000,
    b:'tiger', // ๋ณดํ†ต (,) ๋กœ ๋๋‚ด๋Š” ๊ฒƒ์ด ์—ฌ๋Ÿฌ๋ชจ๋กœ ํŽธํ•˜๋‹ค.
}

console.log(typeof(obj.a.b.c.d));
console.log(obj.a.b.c.d);
number
10

 

๐Ÿ”น๋™์  ํƒ€์ž… ๋ณ€๊ฒฝ

๐Ÿ˜ต but, ๋น„์šฉ ๋ฐœ์ƒ

let apple = 10;
console.log(apple, typeof(apple));

/// ๋‚ด๋ถ€ ์ž‘์—…
// ๋ฉ”๋ชจ๋ฆฌ ์ƒ์„ฑ
// apple = new number();
// ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
// ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
// delete(apple);

// apple = 'tiger'; ๋กœ ์ธํ•ด
// number type์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ญ์ œ๋˜๊ณ  
// string type์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
apple = 'tiger';
console.log(apple, typeof(apple));
10 number
tiger string

 

 

๐Ÿ”นvar  vs  let

var : ์ง€์—ญ์„ฑ์„ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

// var : ์ง€์—ญ์„ฑ์„ ์œ ์ง€ํ•˜์ง€ ์•Š์Œ.
{
    var a = 10;
}
console.log(a);
10

 

let : ์ง€์—ญ์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค.

// let : ์ง€์—ญ์„ฑ์„ ์œ ์ง€ํ•จ.
{
    let b = 20;
    // console.log(b); ๊ฐ€๋Šฅ
}
console.log(b);
์—๋Ÿฌ๋ฐœ์ƒ

 

 

๐Ÿ”นconst

์ƒ์ˆ˜

// const
const a = 10;
console.log(a);
a = 20;
console.log(a);
10
์—๋Ÿฌ๋ฐœ์ƒ

 

 

๐Ÿ”นundefined, null

undefined : ํƒ€์ž…์ด ์ •์˜ ๋˜์ง€ ์•Š์Œ. (return์— ๊ทธ๋‚˜๋งˆ ์ž์ฃผ ์“ฐ์ž„)

null : ํƒ€์ž…์€ ์ •ํ•ด์กŒ์ง€๋งŒ(Object), ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค.

// undefined, null

// ํƒ€์ž…์ด ์ •์˜ ๋˜์ง€ ์•Š์•˜๋‹ค.
// a์˜ ํƒ€์ž…์€ undefined
let a = undefined;

// ํƒ€์ž…์€ ์ •ํ•ด์กŒ๋‹ค.(Object)
// ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค.
let b = null;

console.log(typeof(a));
console.log(typeof(b));
undefined
object

 

 

๐Ÿ”น๋ฌธ์ž์—ด ์—ฐ๊ฒฐ

๋ฌธ์ž์—ด ์—ฐ๊ฒฐ

let str = 'Hello';
str += ' world';
console.log(str);

let s = '100';
let n = 100;
console.log(s + n); // ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ
console.log(typeof(s + n));

console.log(Number(s) + n); 
console.log(typeof(Number(s) + n));

console.log(String(n) + 200);
console.log(typeof(String(n) + 200));
Hello world
100100
string
200
number
100200
string

 

๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜

1. Number()

2. parseInt()

// ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
// 1. Number();
// 2. parseInt();
let s1 = '100';
let s2 = '200';
console.log(Number(s1) + 1);
console.log(parseInt(s2) + 1);

let s3 = '100์›';
let s4 = '200์›';
console.log(Number(s3) + 1);
console.log(parseInt(s4) + 1);
101
201
NaN
201

 

์Šˆ๊ฐ€ ์ฝ”๋“œ

๋ฌธ์ž์—ด ์•ž์— (+)

let s5 = '10';
let s6 = +'10';
let s7 = +s5;
let s8 = 999 + +s5;

console.log(typeof(s5));
console.log(typeof(s6));
console.log(typeof(s7));
console.log(s7);
console.log(typeof(s8));
console.log(s8);
string
2 number
10
number
1009

 

 

๐Ÿ”น์—ฐ์‚ฐ์ž

๐Ÿ™Š ๋ชจ๋‘ ์ž๋ฐ”์™€ ๋ฌธ๋ฒ•์ด ๋™์ผ ํ•˜๋‹ค

(์‚ฐ์ˆ , ๊ด€๊ณ„, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ 
์ฆ๊ฐ€, ๊ฐ์†Œ ์—ฐ์‚ฐ์ž 
+=, ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž 
true, false 
4๋Œ€ ์ œ์–ด๋ฌธ 
์‚ผํ•ญ์—ฐ์‚ฐ)

 

๐Ÿ™‰ ๋‹ค๋ฅธ ๋ถ€๋ถ„

console.log(Math.pow(2, 3));
console.log(2 ** 3);
console.log(2 ** 3 ** 2);
console.log((2 ** 3) ** 2);
2 8
512
64

 

 

๐Ÿ”น์ง„์ˆ˜, ์ง„๋ฒ•

let a = 10;
let b = 0x10; // 16์ง„์ˆ˜ ํ‘œ๊ธฐ
let c = 0o777; // 8์ง„์ˆ˜ ํ‘œ๊ธฐ
let d = 0b1111011; // 2์ง„์ˆ˜ ํ‘œ๊ธฐ

console.log(a);
console.log(b);
console.log(c);
console.log(d);
10
16
511
123

 

 

๐Ÿ”น==, ===

== : ๊ฐ’์ด ๊ฐ™์€๊ฐ€?

=== : ๊ฐ’๊ณผ ํƒ€์ž… ๋ชจ๋‘ ๊ฐ™์€๊ฐ€?

// ๊ฐ’์ด ๊ฐ™์€๊ฐ€?
console.log(10 == 10);

// ๊ฐ’์ด ๊ฐ™๊ณ  ํƒ€์ž…๋„ ๊ฐ™์€๊ฐ€?
console.log(10 === '10');
console.log(10 == '10');
true
false
true

 

 

๐Ÿ”น/ ,  %

console.log(7 / 4); // ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.
console.log(7 % 4);
1.75
3

 

 

๐Ÿ”น' ', " ", ` `

 ' ' : ๋ฌธ์ž์—ด

" " : ๋‚ด๋ถ€์— ' ' ํ‘œํ˜„ ๊ฐ€๋Šฅ

` ` : ๋‚ด๋ถ€์— ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

console.log('ํ•œ๊ธ€');
console.log("ํ•œ๊ธ€'๊ณผ'์ปดํ“จํ„ฐ");

let first = 'tiger';
let last = 'lion';
console.log(`My name is ${first} or ${last}`);

let a = 3;
let b = 4;
console.log(`${a + b}`);
console.log(`${a} + ${b} = ${a + b}`);
ํ•œ๊ธ€
ํ•œ๊ธ€'๊ณผ'์ปดํ“จํ„ฐ
My name is tiger or lion
7
3 + 4 = 7

 

 

 

๐Ÿ”นSymbol

์ฃผ๋กœ ์ด๋ฆ„์ด ์ถฉ๋Œํ•  ์œ„ํ—˜์ด ์—†๋Š” ๊ฐ์ฒด์˜ ์œ ์ผํ•œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

์‹ฌ๋ฒŒ ๊ฐ’์€ ๋‹ค๋ฅธ ๊ฐ’๊ณผ ์ค‘๋ณต ๋˜์ง€ ์•Š๋Š” ์œ ์ผ๋ฌด์ดํ•œ ๊ฐ’์ด๋‹ค.

// SYMBOL type

// 1)
let obj = {
    a : 10,
};

// ์‹คํ–‰์‹œ๊ฐ„์— ํ•„์š”์— ๋”ฐ๋ผ์„œ ํ‚ค๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
obj.b = 20;
console.log(obj.a);
console.log(obj.b);
console.log('----------');


// 2)
let obj2 = {
    a : 10,
};

// ์‹คํ–‰์‹œ๊ฐ„์— ํ•„์š”์— ๋”ฐ๋ผ์„œ ํ‚ค๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
obj2['b'] = 20;
console.log(obj2['a']);
console.log(obj2['b']);
console.log('----------');



// 3) obj3 ๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณต ๋œ ๊ฐ์ฒด ์ผ ๋•Œ.
// obj3['b'] = 30; ๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ฒƒ์ธ์ง€
// ๊ธฐ์กด์— ์žˆ๋˜ ๊ฒƒ์„ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค.
let obj3 = {
    a : 10,
    b : 20,
};

obj3['b'] = 30;
console.log(obj3['a']);
console.log(obj3['b']);
console.log('----------');


// 4)
let obj4 = {
    a : 10,
    b : 20,
    myfunc:function(){
        return this.a + this.b;
    },
};

// ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๋ณธ์ธ์€ ํ‚ค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋”ฉ์„ ํ•˜์˜€๋‹ค.
// ํ•˜์ง€๋งŒ b๊ฐ’์ด ์ค‘์ฒฉ๋œ ๊ฒƒ์ธ์ง€ ์•„๋‹Œ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.
// ๊ต‰์žฅํžˆ ์• ๋งค๋ชจํ˜ธํ•œ ์ƒํ™ฉ(obj4๋Š” ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋…ธ์ถœ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๊ณ  ๊ฐ€์ •.)
obj4['b'] = 30;
console.log(obj4.myfunc(), obj4['b']);
console.log('----------');


// 5)
let obj5 = {
    a : 10,
    b : 20,
    myfunc:function(){
        return this.a + this.b;
    },
};

let b = Symbol('b');
obj5[b] = 999;
// ํ•จ์ˆ˜๋Š” ๋ณธ์ธ์ด ๊ฐ’์„ ๋ฐ”๊พธ๋“  ์•ˆ๋ฐ”๊พธ๋“  ์œ ์ผํ•˜๊ฒŒ ์‹คํ–‰๋œ๋‹ค.
console.log(obj5.myfunc(), obj5[b]);
10
20
----------
10
20
----------
10
30
----------
40 30
----------
30 999

 

 

 

'IT EDU > JAVA SCRIPT' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] map, reduce ๋ฉ”์†Œ๋“œ  (2) 2022.03.23
[JavaScript] JavaScript ํ™˜๊ฒฝ ์„ค์ •  (0) 2022.03.21

๋Œ“๊ธ€