프로그래밍/Node.js
[JavaScript] 자바스크립트 기초 공부! (변수와 상수/연산자/데이터타입/객체/배열/형변환)
제이스톨
2023. 5. 24. 09:40
728x90
정보처리기사 4과목 java 설명과 마찬가지로 변수, 연산자, 함수부터 시작합니다.
시작해보겠습니다!
1. 변수와 상수
: 메모리에 저장하고 읽어드린다
let start = "hello World";
console.log(start);
이렇게 치면 콘솔 창에 hello world가 찍히게 됩니다.
2-1. 데이터 타입
: string, num, boolean...등등
// ※ java : String a = "abc"
let num = 10;
console.log(num);
console.log(typeof num); // typeof : 데이터타입 확인 가능
콘솔 값 입력할 때 typeof를 붙이면 변수의 데이터 타입을 확인 할 수가 있습니다.
2-2. 문자열
let str = "hello world";
console.log(str.length); // 문자열 길이 확인 가능
let str1 = "hello";
let str2 = "world";
let result = str1.concat(str2); // hello + world 합치기
console.log(result);
let str3 = "hello world";
console.log(str3.substr(7,5)); // 문자열을 짜름
let str4 = "hello world";
console.log(str.search("world")); // world가 시작되는 지점을 찾음
2-3. boolean type
: 콘솔 값이 true 아니면 false로 나오는 값을 말합니다.
3. undefined
: 값이 할당안된 것을 의미합니다..
[ 여기서 주의할 것 - null : 값이 존재하지 않음을 "명시적"으로 나타냄 (개발자가 일부러 만든 것) ]
let x;
console.log(x); // undefined
let y = null;
console.log(y); // null
4. object (객체)
let person = {
name : 'choi',
age : 20,
isMarried : true
}
console.log(person);
console.log(typeof person);
"choi, 20살, 기혼" 라는 객체 값을 넣어준 모습입니다.
5. array (배열)
: 값을 쭉~~~ 나열한 것이라고 생각하면 편합니다.
let number = [1,2,3,4,5];
console.log(number);
이렇게 나타내면 콘솔창에 1,2,3,4,5가 찍히게 됩니다.
6. cast (형변환)
: 형태를 바꾼다 -> 암시적, 명시적 형변환
let result1 = 1 + "2";
console.log(result1);
console.log(typeof result1); // 문자열이 우선 시 된다
let result2 = "2" + "3";
console.log(result2); // 23이 출력되게 된다
7. 연산자
: 쉽게 말해 더하기, 빼기, 곱하기, 나누기라고 생각하면 됩니다.
console.log(2 + 3);
console.log(2 / 3); // 나누기
console.log(2 % 3); // 나머지 값
console.log(==========경계선==========);
let z = 10;
z += 5;
console.log(z); // 10 + 5 = 15

728x90