💭 typeof
: 자료형 검사
<script>
// 숫자 타입
var intNum = 10;
var floatNum = 0.1;
// 문자열 타입
var singleQuoteStr = 'single quote string';
var doubleQuoteStr = "double quote string";
var singleChar = 'a';
// 불린 타입
var boolVar = true;
// undefined 타입
var emptyVar;
// null 타입
var nullVar = null;
console.log(
typeof intNum, // number
typeof floatNum, // number
typeof singleQuoteStr, // string
typeof doubleQuoteStr, // string
typeof singleChar, // string
typeof boolVar, // boolean
typeof emptyVar, // undefined
typeof nullVar // object
);
</script>
<script>
var num = 5 / 2;
console.log(num); // 2.5
console.log(Math.floor(num)); // 2
</script>
<script>
// str 문자열 생성
var str = 'test';
console.log(str[0], str[1], str[2], str[3]); // t e s t
// 문자열의 첫 글자를 대문자로 변경?
str[0] = 'T';
console.log(str); // test
</script>
console.log('가나다' + '라마' + '바사아'); // 가나다라마바사아
console.log('안녕하세요'[0]); // 안
console.log('안녕하세요'[3]); // 세
console.log('안녕하세요'.length); // 5
console.log(''.length); // 0
var txt = 'Hello~ Hong~';
console.log(txt.indexOf('~')); // 5
console.log(txt.indexOf('~', 6)); // 11
console.log(txt.lastIndexOf('~')); // 11
var txt = 'Hello~ Hong~';
console.log(txt.toUpperCase()); // HELLO~ HONG~
console.log(txt.toLowerCase()); // hello~ hong~
var txt = 'Hello~ Hong~';
console.log(txt.substring(2, 8)); // llo~ H
console.log(txt.substring(2)); // llo~ Hong~
// 글자수
console.log(txt.substr(2, 4)); // llo~
console.log(txt.substr(2)); // llo~ Hong~
// 문자
console.log(txt.charAt(2)); // l
// 문자 코드값
console.log(txt.charCodeAt(2)); // 108
txt = 'Hello~ Hong~ Bye~ Hong~';
console.log(txt.replace('Hong', 'Lee')); // Hello~ Lee~ Bye~ Hong~
console.log(txt.replace(/Hong/gi, 'Lee')); // Hello~ Lee~ Bye~ Lee~
txt = ' 하나 둘 셋 ';
console.log('@' + txt.trim() + '@'); // @하나 둘 셋@
console.log('@' + txt.trimStart() + '@'); // @하나 둘 셋 @
console.log('@' + txt.trimEnd()+ '@'); // @ 하나 둘 셋@
txt = '홍길동,아무개,호호호,테스트,하하하';
console.log(txt.split(','));
[출력결과]
(5) ['홍길동', '아무개', '호호호', '테스트', '하하하']
0: "홍길동"
1: "아무개"
2: "호호호"
3: "테스트"
4: "하하하"
length: 5
txt = '홍길동';
console.log(txt.startsWith('홍')); // true
console.log(txt.endsWith('동')); // true
💭 이스케이프 문자 : 특수한 기능을 수행
- \n : 줄바꿈
- \t : 탭
- \\ : 역슬래시(\)
- \" : 이중따옴표
- \' : 단일따옴표
값이 비어있음
을 나타낸다.null 타입 변수인지를 확인할 때
는 typeof 연산자 X, 일치 연산자(===)를 사용
한다.<script>
// null 타입 변수 생성
var nullVar = null;
console.log(typeof nullVar === null); // false
console.log(nullVar === null); // true
</script>
💭 비교 연산자
== : 자료형 비교(x), 값만 비교(o)
=== : 자료형 비교(o) + 값 비교(o)
// Object()를 이용해서 foo 빈 객체 생성
var foo = new Object();
// foo 객체 프로퍼티 생성
foo.name = 'foo';
foo.age = 30;
foo.gender = 'male';
console.log(typeof foo); // object
console.log(foo); // { name: 'foo', age: 30, gender: 'male'}
foo['name'] = 'su yeon';
foo.home-tel = '010-1234-5677'; // 식별자 기호 X
foo['home-tel'] = '010-1234-5677'; // 식별자 기호 O > 웬만하면 식별자 기호 쓰지말기
{}
이용해서 객체를 생성한다.// 객체 리터럴 방식으로 foo 객체 생성
var foo = {
name: 'foo',
age: 30,
gender: 'male'
};
console.log(typeof foo); // object
console.log(foo); // { name: 'foo', age: 30, gender: 'male'}
const jung = {
name: '정형돈',
age: 30,
address: {
sido: '서울시',
gugun: '강동구',
dong: '천호동'
}
};
console.log(jung.name);
console.log(jung.age);
console.log(jung.address); // { sido: '서울시', gugun: '강동구', dong: '천호동'}
console.log(jung.address.sido);
parseInt(value)
parseFloat(value)
var n1= 3.99;
console.log(parseInt(n1)); // 실수 -> 정수
var n2 = '300';
console.log(parseInt(n2)); // 문자열 -> 정수
var n3 = '100점';
console.log(parseInt(n3)); // 100점
var n4 = '스코어90';
console.log(parsInt(n4)); // NaN -> Not a Number
var n5 = '100점이 아니라 50점입니다.'
cosole.log(parsenInt(n5)); // 100점
var width = "150px";
// 잘못된 조작 X
console.log(width * 2); // NaN (Not a Number)
var width = `200px`;
console.log(parseInt(width) + 100);
var size = '1.5em';
console.log(parseInt(size));
console.log(parseFloat(size));
// 자주 사용하는 곳 > 유효성 검사(숫자만 입력했니?)
var age = '20세';
console.log(isNaN(age)); // false
if (isNaN(age)) {
console.log('나이가 올바르지 않습니다.');
}else {
console.log('올바른 나이입니다.');
}