js-tutorial-2

dev-riverkim·2023년 7월 7일
0
// 5_data_types.js

/**
 * Data Types
 * 
 * 여섯개의 Primitive Type과
 * 한개의 오브젝트 타입이있다.
 * 
 * 1) Number (숫자)
 * 2) String (문자열)
 * 3) Boolean (불리언)
 * 4) undefined (언디파인드)
 * 5) null (널)
 * 6) Symbol (심볼)
 * 
 * 7) Object (객체)
 *    Function
 *    Array
 *    Object
 */

/**
 * Number 타입
 */
const age = 32;
const tempature = -10;
const pi = 3.14;

console.log(typeof age); // number
console.log(typeof tempature); // number
console.log(typeof pi); // number
console.log('--------------');

const infinity = Infinity; 
const nInfinity = -Infinity; 

console.log(typeof infinity);// number
console.log(typeof nInfinity);// number
console.log('--------------');

/**
 * String 타입
 */
const riverKim = '"ri"verkim';
console.log(riverKim); // "ri"verkim
console.log(typeof riverKim); // string

const ive = "'아이브' 안유진";
console.log(ive); // '아이브' 안유진

/**
 * Template Literal
 * 
 * Escaping Character
 * 1) newline -> \n
 * 2) tab -> \t
 * 3) 백슬래시를 스트링으로 표현하고싶다면 두번 입력하면된다.
 */
const iveYuJin = '아이브\n안유진'
console.log(iveYuJin); // 아이브
                       // 안유진
const iveWonYoung = '아이브\t장원영'; 
console.log(iveWonYoung); // 아이브 장원영
const backSlash = '아이브\\뉴진스';
console.log(backSlash);// 아이브\뉴진스
const smallQutoation = '아이브\'뉴진스';
console.log(smallQutoation); // 아이브'뉴진스

const iveWonYoung2 = `아이브i '" / / / / / /
장원영`;
console.log(iveWonYoung2); // 아이브i '" / / / / / /
                           //장원영

console.log(typeof iveWonYoung2); // string

const groupName = '아이브'; 
console.log(groupName + ' 안유진'); // 아이브 안유진
console.log(`${groupName} 안유진`); // 아이브 안유진
console.log('-------------');

/**
 * Boolean 타입
 */
const isTrue = true;
const isFalse = false;
console.log(typeof isTrue); // boolean
console.log(typeof isFalse); // boolean

/**
 * undefined
 * 
 * 사용자가 직접 값을 초기화하지 않았을때
 * 지정되는 값이다.
 * 
 * 직접 undefined로 값을 초기화하는건 지양해야한다.
 * let noInit = undefined; -> XXX 하면 안됨
 * '지향'은 하고자 하는 것, '지양'은 하지 않고자 하는 것
 */
let noInit;
console.log(noInit); // undefined
console.log(typeof noInit); // undefined

/**
 * null 타입
 * 
 * undefined와 마찬가지로 값이 없다는 뜻이나
 * JS에서는 개발자가 명시적으로 없는 값으로 초기화할때
 * 사용된다.
 */
let init = null;
console.log(init); // null
console.log(typeof init); // object -> 자바스크립트 버그
// 원래는 null 타입으로 출력되야 한다.
console.log('----------')

/**
 * Symbol 타입
 * 
 * 유일무이한 값을 생성할때 사용한다.
 * 다른 프리미티브 값들과 다르게 Symbol 함수를
 * 호출해서 사용한다.
 */
const test1 = '1';
const test2 = '1';

console.log(test1 === test2); // true

const values = test1 === test2; 
console.log(values); // true
console.log(typeof values); // boolean

const symbol1 = Symbol('1');
const symbol2 = Symbol('1');

console.log(symbol1 === symbol2); // false

/**
 * Object 타입
 * 
 * Map
 * 키:벨류의 쌍으로 이루어져있다.
 * key:value
 */
const dictionary = {
    red: '빨간색',
    orange: '주황색',
    yellow: '노란색',
    color1: '컬러1',
    'color2': '컬러2',
};

console.log(dictionary); // { red: '빨간색', orange: '주황색', yellow: '노란색' }
console.log(dictionary['red']); // 빨간색
console.log(dictionary['orange']); // 주황색
console.log(dictionary['yellow']); // 노란색
console.log(dictionary['color1']); // 컬러1
console.log(dictionary['color2']); // 컬러2

console.log(typeof dictionary); // object

/**
 * Array 타입
 * 
 * 값을 리스트로 나열 할 수 있는 타입이다.
 */
const iveMembersArray = [
    '안유진',
    '가을',
    '레이',
    '장원영',
    '리즈',
    '이서',
];
console.log(iveMembersArray); // [ '안유진', '가을', '레이', '장원영', '리즈', '이서' ]

/**
 * index
 * 
 * 0부터 시작한다.
 * 1씩 올라갑니다.
 */
console.log(iveMembersArray[0]); // 안유진
console.log(iveMembersArray[5]); // 이서
console.log(iveMembersArray[6]); // undefined

iveMembersArray[0] = 'riverkim';
console.log(iveMembersArray); // [ 'riverkim', '가을', '레이', '장원영', '리즈', '이서' ]
console.log(typeof iveMembersArray); // object

/**
 * static typing -> 변수를 선언할때 어떤 타입의 변수를 선언할지 명시를한다.
 *                  C
 * dynamic typing -> 변수의 타입을 명시적으로 선언하지 않고 갑에의해 타입을 "추론"한다.
 * JS -> dynamic typing
 */
profile
dev-riverkim

0개의 댓글