[JS] localStorage ๋กœ ํžˆ์Šคํ† ๋ฆฌ ๋‚จ๊ธฐ๊ธฐ
728x90

localStorage ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€

๋ธŒ๋ผ์šฐ์ € ๋‚ด์— ํ‚ค-๊ฐ’์„ ์Œ์œผ๋กœ ํ•˜์—ฌ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ค์‹œ ๋‹ซํžŒ ๋’ค ๋‹ค์‹œ ์‹คํ–‰๋˜์–ด๋„ ๊ฐ’์ด ์œ ์ง€๋œ๋‹ค.

ํ‚ค-๊ฐ’์€ ํ•ญ์ƒ ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•œ๋‹ค.

 

๋˜ ๋„๋ฉ”์ธ์ด๋‚˜ ํฌํŠธ, ํ”„๋กœํ† ์ฝœ์ด ๊ฐ™๋‹ค๋ฉด ๋™์ผํ•œ ๋ชจ๋“  ์ฐฝ์—์„œ ๊ณต์œ ๋œ๋‹ค.

 

 

๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์˜ ๊ธธ์ด

ํ˜„์žฌ ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

const localLength = localStorage.length;

 

localStorage.setItem

๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

localStorage.setItem(keyword);

 

ํ‚ค ๊ฐ’ ์ „์ฒด ์•Œ๊ธฐ

 for๋ฌธ์„ ํ†ตํ•ด ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค์— ์ ‘๊ทผํ•˜์—ฌ

localStorage.getItem(key) ๋ฅผ ํ†ตํ•ด ํ‚ค์˜ ๊ฐ’์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

for (let i = 0; i <= localLength; i++) {
	let key = localStorage.key(i);
	console.log(`key: ${key}`);
	let thisKeyword = localStorage.getItem(key);
		
	console.log(`${i}๋ฒˆ ํ‚ค์›Œ๋“œ: ${thisKeyword}`);
	
}

 

 

localStorage.removeItem

๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์˜ ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค

localStorage.removeItem(key);

 

 

 

๐Ÿ“ƒ์ฐธ๊ณ 

 

https://developer.mozilla.org/ko/docs/Web/API/Window/localStorage

 

Window.localStorage - Web API | MDN

localStorage ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด Document ์ถœ์ฒ˜์˜ Storage ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ธŒ๋ผ์šฐ์ € ์„ธ์…˜ ๊ฐ„์— ๊ณต์œ ๋ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

https://ko.javascript.info/localstorage

 

localStorage์™€ sessionStorage

 

ko.javascript.info

 

728x90