자바스크립트 기초1

dev-riverkim·2023년 6월 8일
0

console.log('hello world'); // hello world
console.log('hello','world'); // hello world



// 코멘트를 작성하는 첫번째 방법 
// 코멘트(주석)은 사람이 보기 위해 작성
console.log('hello world'); // 코드에 대한 설명

/**
 * 코멘트를 여러 줄 작성할 수 있다.
 * 여러 줄 코멘트가 가능함
 *
 */


/**
* 변수(variable) 선언하기
* 1) var -> 더 이상 쓰지 않음
* 2) let
* 3) const
*/

var name = 'riverkim';
console.log(name); // riverkim

var age = 32;
console.log(age); // 32

let ive = '아이브';
console.log(ive); // 아이브


const newJeans = '뉴진스';
console.log(newJeans); // 뉴진스
// newJeans = 'riverkim'; // Error: Assignment to constant variable.


/**
 * 선언과 할당
 * 
 * 
 * 1)선언은 변수를 선언하는것 -> var name; 
 * 2)할당 -> var name ='값';
 * 
 * 변수를 선언하고 할당하지 않는 경우도 있다.
 * 변수를 선언만 하고 할당하지 않으면 초깃값이 undefined가 된다.
 * const에서는 변수를 선언하고 할당하지 않으면 오류가 난다. 
 * -> const는 값이 할당된 후 재할당이 되지 않으므로 undefined가 할당되면 변경할 수 없다.
 * 
 */

var name = 'riverkim'; // 변수의 선언과 할당이 동시에 이루어짐
console.log(name);  // riverkim

let emptyValue; // 변수를 선언만 하고 할당하지 않으면 초깃값이 undefined가 된다.
console.log(emptyValue);// undefined



/**
 * Naming Conventions
 * 
 * 변수 이름 지을때
 * 1) 일반적으로 영어(라틴문자)를 사용하며 문자와 숫자를 모두 사용 할 수 있다.
 * 2) 특수기호는 언더스코어와 달러를 사용 할 수 있다.
 * 3) 숫자로 이름을 시작 할 수 없다.
 *    1Name, 2Hello
 * 4) 키워드는 변수명으로 사용 할 수 없다.
 *    var var = 'var';
 *    var const = 'const';
 * 
 * 외우지 않아도 된다.
 */

let riverkim = 'riverkim';
var $ive = '아이브';
const _yuJin = '안유진';

console.log(riverkim); // riverkim
console.log($ive); // 아이브
console.log(_yuJin); // 안유진

// let 1name = 'name' -> 오류
// let const = 'attention'; -> 오류


/**
 * Naming Convention 2
 * 
 * 1) camelCase -> 대부분의 언어에서 많이 사용
 * 2) snake_case -> 파이썬같은 언어에서사용
 * 3) PascalCase -> C# 마이크로소프트 계열의 언어에서 사용함
 */


const anYuJin = '안유진';
console.log(anYuJin); // 안유진


/**
 * 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 = '"리"버킴';
console.log(riverkim); // 리버킴
console.log(typeof codeFactory); // string

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

/**
 * Template Literal
 *
 * Escaping Character
 * 1) newline -> \n
 * 2) tab -> \t
 * 3) 백슬래시를 스트링으로 표현하고싶다면 두번 입력하면된다.
 */


const iveYuJin = "아이브\n안유진22";
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);

console.log(typeof iveWonYoung2);

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;
console.log(noInit); // undefined
console.log(typeof noInit); // undefined


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


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

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

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

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


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

console.log(dictionary);
console.log(dictionary["red"]);
console.log(dictionary["orange"]);
console.log(dictionary["yellow"]);

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개의 댓글