TIL/멋쟁이사자처럼 FE스쿨 2기
[Day 25] JS 변수와 String, Boolean, Object 자료형
Dahee.jo
2022. 5. 2. 20:40
SMALL
기본적으로 알아야할 컴퓨터 공학 지식
<!--
1bit = 0과 1을 표현할 수 있는 최소 단위
8bit = 1byte
00000000 ~ 11111111 (0 ~ 255)
ex) 0.0.0.0 ~ 255.255.255.255
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
// (KB,MB,GB,TB,PB)갈매기털빼
-->
// computer의 10진수 연산은 정확하지 않다.
// 왜? =>부동소수점 이슈
// 0.1 + 0.2
// 0.30000000000000004
// 0.1 * 3
// 0.30000000000000004
// 0.3 + 0.6
// 0.8999999999999999
// JS에서 큰 숫자를 사용할 때에는 BigInt를 사용
// 1231231231231231233 + 2 = 1231231231231231200 ??
// BigInt(1231231231231231233) + BigInt(3)
String
1. parseInt()와 Number()의 차이?
let txt = "ABCDEFGHIJKLMABCF"
let txt2 = 'mom said \'hello world\''
let number = '100'
console.log(Number(number)+Number(number))
console.log(number+number);
console.log(parseInt(number)+parseInt(number))
// parseint가 1순위 권장
Number() 와 parseInt() 둘 다 숫자로 형변환을 한다는 것은 같지만,
차이점은
Number 는 어떤 값이던 숫자형 자료형으로 강제하여 바꾸는 반면,
parseInt는 오직 string만을 파싱한다.(만약 스트링이 아니면 스트링으로 먼저 바뀐다)
또한 2번째 파라미터로 radix가 온다.(이진수, 8진수 등..)
2. indexOf() search()의 차이?
console.log(txt.indexOf("F")) //5
console.log(txt.search("FG")) //5
console.log(txt.search("Z")) //-1
let regExp = /BCD/;
console.log(`txt.search(regExp):${txt.search(regExp)}`);
둘 다 각 문자열에 맞는 인덱스를 찾는다는 점에서 공통점이 있지만
indexOf는 정규표현식에서는 사용할 수 없다!
Boolean
let value1=true
let value2=false
console.log(!value2); //true
console.log(!!value2); //false
*혼란스러운 것들...*
console.log(`8. ${0 == ''}`); // 주의 true
console.log(`9. ${false == ''}`); // 주의 true
console.log(`10. ${false == null}`); // 주의 false
console.log(`11. ${false == undefined}`); // 주의 false
console.log(`12. ${NaN == NaN}`); // false 주의
console.log(`13. ${NaN === NaN}`); // false 주의
console.log(`14. ${isNaN(undefined)}`); // true 주의
console.log(`15. ${isNaN(null)}`); // false 주의
console.log(`16. ${isNaN(NaN)}`); // true 주의
console.log(`17. ${Number.isNaN(undefined)}`); // true 주의
console.log(`18. ${Number.isNaN(null)}`); // true 주의
console.log(`19. ${Number.isNaN(NaN)}`); // true 주의
console.log(`20. ${![]}`); // false 주의
console.log(`21. ${!{}}`); // false 주의
if ([]) {
console.log('hello world 1'); //출력
}
if ({}) {
console.log('hello world 2'); //출력
}
if ('') {
console.log('hello world 3'); //미출력
}
if (undefined) {
console.log('hello world 4'); //미출력
}
Object
let txt = 'hello'
let txt2 = {
0: 'h',
1: 'e',
2: 'l',
3: 'l',
4: 'o',
}
console.log(txt[1]); //e
// console.log(txt.1); //error
console.log(txt2[1]); //e
// 이거 2개를 해줄 수 있는게 map
// console.log(txt2.1);
// console.log(txt2.{'one':'1'});
error 뜬 저 문이 이해가 가질 않았지만 일단 정리해본다.
오늘 수업의 느낀 점
본격적인 우주여행의 시작...
문법이 더럽기로 유명한 자스라 그런지 헷갈리는게 매우 많았다...!
그래서 오늘 TIL도 이해는 안가지만 모르는 내용으로 정리하려다 보니 설명이 부족한것같다...ㅎㅎ
차츰 차츰 오늘의 물음표가 미래엔 풀리길 !!
반응형