변수/상수
[변수의 5가지 주요 개념]
변수 이름: 저장된 값의 고유 이름
변수 값: 변수에 저장된 값
변수 할당: 변수에 값을 저장하는 행위
변수 선언: 변수를 사용하기 위해 컴퓨터에 알리는 행위
변수 참조: 변수에 할당된 값을 읽어오는 것
[변수를 선언할 수 있는 3가지 방법]
var let const
특징)
var는 다시 변수를 선언할 수 있다. > var myVar = "hello wolrd";
var myVar = "hello wolrd 2";
정상적으로 변수가 선언된다.(var myVar = "hello wolrd 2";)
var와 let은 다시 변수를 할당할 수 있다. > var myVar = "hello wolrd";
myVar = "hello wolrd 2";
let myLet = "hello wolrd 3";
myLet = "hello wolrd 4";
정상적으로 변수가 할당된다.(myVar = "hello wolrd 2" / myLet = "hello wolrd 4";)
데이터 타입
데이터 타입을 알 수 있는 명령어(typeof)
숫자형
let num1=10; 정수/
let num2=3.14; 실수/
let num3=2.5e5; //2.5*10^5// 문자가 포함된 지수형/ 다 숫자형 데이터 타입이다.
console.log(typeof num1)=number
console.log(typeof num2)=number
console.log(typeof num3)=number
let num4="hello" / 2 ;
console.log(typeof num4)=NaN 문자형을 나눌 수 없기 때문에 결과 값이 NaN이 나온다.(Not a Number)
let num5= 1 / 0 ;
console.log(num5)=Infinity
console.log(typeof num4)=number 무한대도 숫자형이다.
let num5= -1 / 0 ;
console.log(num6)= - Infinity
console.log(typeof num4)=number 마이너스 무한대도 숫자형이다.
문자형
문자열 길이 확인하기(length)
let str= "hello wolrd";
console.log(str)= hello world
console.log(typeof str)=string 문자형으로 나온다.
console.log(str.length)= 11 문자열의 개수가 나온다.
문자열 결합하기(concat)
let str1= "hello, ";
let str2= "wolrd";
let result= str1.concat(str2);
console.log(result)= "hello, wolrd " 문자열이 결합된다.
문자열 자르기(substr,slice)
let str3= "hello, wolrd";
console.log(str3.substr(7,5))= "wolrd" 7번째까지 자르고 5개까지 출력해라.
console.log(str3.slice(7,11))= "wolrd" 7번째까지 자르고 11번째까지 출력해라.
문자열 검색(search)
let str4= "hello, wolrd";
console.log(str4.search("wolrd"))=7 "wolrd"가 시작되는는 지점
문자열 대체(replace)
let str5= "hello, wolrd";
let result= str5.replace("wolrd", "javascript");
console.log(result)=hello, javascript "wolrd"가 "javascript"로 바뀐다.
문자열 분할(split)
let str6= "apple, banana, kiwi";
let result= str6.split(","); => 자르는 기준이 , 로 설정
console.log(result)=['apple', 'banana', 'kiwi']로 바뀐다.
불린형
let bool1= true;
let bool2= false;
console.log(bool1)=true
console.log(typeof bool1)=boolean
console.log(bool2)=false
console.log(typeof bool2)=boolean
undefined
let x; //값을 할당하지 않음//
console.log(x)=undefined
null
let y = null
console.log(y)=undefined
object(객체)
let person = {
name : "choi"
age : 20
isMarried : true
};
console.log(typeof person)=object
array(배열)
let numbers = [1,2,3,4,5];
let fruits = ['apple', 'banana', 'orange'];
console.log(typeof fruits)=object
형변환
숫자 형 변환
let result1 = 1+"2"
console.log(result1)=12
console.log(typeof result1)=string 숫자열과 문자열이 합쳐지고 결과값은 문자형으로 결정
let result2 = "1"+true ;
console.log(result2)=1true
console.log(typeof result2)=string 문자열과 불린형이 합쳐지고 결과값은 문자형으로 결정
let result3 = 1-"2" ;
console.log(result3)=-1
console.log(typeof result3)=number 숫자열과 문자열이 빼지고 결과값은 숫자형으로 결정
let result4 = "2"*"2" ;
console.log=4
console.log(typeof result4)=number 문자열과 문자열이 곱해지고 결과값은 숫자형으로 결정
//더하기 연산자를 제외한 연산자는 결과값이 숫자형이 우선시 된다.//
명시적 형 변환
불린
console.log(Boolean(0))=false
console.log(Boolean(""))=false
console.log(Boolean(null))=false
console.log(Boolean(undefined))=false
console.log(Boolean(NaN))=false
console.log(Boolean("false"))=true
console.log(Boolean({}))=true
문자열
let result1 = Stirng(123)
console.log(result1)=123
console.log(typeof result1)=string
let result2 = Stirng(false)
console.log(result2)=false
console.log(typeof result2)=string
let result3 = Stirng(null)
console.log(result3)=null
console.log(typeof result3)=string
let result4 = Stirng(undefined)
console.log(result4)=undefined
console.log(typeof result4)=string
숫자열
let result1 = Number("123")
console.log(result1)=123
console.log(typeof result1)=number
'JavaScriprt-문법' 카테고리의 다른 글
JavaScript 문법 종합반 문법-객체 (0) | 2023.05.22 |
---|---|
JavaScript 문법 종합반 문법-조건문 (0) | 2023.05.22 |
JavaScript 문법 종합반 문법-함수 (0) | 2023.05.22 |
JavaScript 문법 종합반 문법-연산자 (0) | 2023.05.22 |
JavaScript 문법 종합반 문법-JS의 역사 (0) | 2023.05.22 |