Data Type

Ming·2023년 7월 19일

JavaScript

목록 보기
2/15

👀 JS는 타입이 있다!
👀 데이터 타입은 프로그래밍 언어에서 사용할 수 있는 데이터의 종류이다

Data Type

  • 원시타입(primitive data type)
    :변경 불가능한 값이며 값에 의해 전달되는 값이다.

    • number
    • string
    • boolean
    • null
    • undefined
    • symbol
  • 객체타입(object/reference type)
    : 이름과 값을 가지는 데이터를 의미하는 프로퍼티(property)와 동작을 의미하는 메소드(method)를 포함할 수 있는 독립적 주체

    • object

1. 원시타입

1-1. number(숫자)

  • integer(정수) ex) 1, 2, 3, 4 … / float(소수) ex) 0.1, 1.5, 3.2 …
  • 덧셈( + ) / 뺄셈( - ) / 나눗셈( / ) / 곱셈( * ) / 나머지 값( % )

1-2. string(텍스트)

  • 처음부터 끝까지 모두 문자로 구성됨
  • "값”(쌍따옴표 안에 문자 값 입력)

1-3. boolean(참/거짓)

  • true: 값이 맞음
  • false: 값이 틀림
    • 0, 비어있는 문자열, null, undefined는 false로 간주된다.

1-4. null

: 값이 없음(변수에 “값이 없음”을 할당)

  • null / Null / NULL 다르다!

1-5. undefined

: 값이 없음(변수에 “값”을 할당하지 않음) ←메모리 상 변수 자리는 존재하나 값은 채워지지 않음

1-6. symbol(ES6 추가)

~좀 더 공부후에 업데이트!~

MDN

📝 Method

typeof()
: 해당 type를 알기

isNaN()
: 숫자가 아님을 판별(숫자=false / 글자=ture)

prototype.length
: 해당 길이나 갯수 알기

const num = 100;
const text = "100";
console.log(typeof num) // number
console.log(typeof text) // string
// a는 숫자 / b는 텍스트 => a와 b의 값은 다르다
  
const loogtext = "abcdefghijk"
const spacetext = "a b c d e f g h i j k"
console.log(loogtext.length); // 11
console.log(spacetext.length); // 21 <- 공백도 포함된다!

const no = null;
const un = undefined;
console.log(typeof no); // object <- 자바스크립트의 오류! null이 맞다.
console.log(typeof un); // undefined

// 변수 no는 null값이 맞니?
console.log(typeof no === null); // false 아니
console.log(no === null); // true 응

// => null값을 알기 위해서는 일치연산자(===) 사용!

Number()
: string → number로 변환

String()
: number → string 변환

prototype.toUpperCase()
: 알파벳 소문자 → 대문자로 변환

prototype.toLowerCase()
: 알파벳 대문자 → 소문자 변환

// number → string 변환하기
function nonum(n){
	let answer = '';
    answer = String(n)
    return answer;
}
console.log(nonum(123)); // 123
console.log(typeof(nonum(567))); // string

// 알파벳 소문자 → 대문자로 변환하기
function ENG(alphabet){
    return alphabet.toUpperCase();
}
console.log(ENG("AbCdEfG")); // ABCDEFG

String.prototype.padStart()
: string.padStart(총 문자열의 자릿수,"총 문자열의 자릿수를 만들기 위해 처음부터 넣을 string")
: 문자열이 지정된 길이에 도달할 때까지 현재 문자열을 다른 문자열로 채움(패딩은 현재 문자열의 처음부터 적용)

String.prototype.padEnd()
: string.padEnd(총 문자열의 자릿수,"총 문자열의 자릿수를 만들기 위해 끝부터 넣을 string")
: 문자열이 지정된 길이에 도달할 때까지 현재 문자열을 다른 문자열로 채움(패딩은 현재 문자열의 끝부터 적용)

prototype.substring()
: substring(start 기준 값, 끝 위치)
: 문자열 시작점을 지정해 자르기

String.prototype.split()
: split(split할 기준 값)
: 특정 기준 값을 정해 잘라 배열에 넣어주기

let num = 1;
num.padStart(2,"0")
// "01"
// 총 2자리수, 현재 2자리가 되지 않는다면 앞에 0을 넣어 두자리로!

const a = "ABCDEFG"
a.substring(2)
// -> CDEFG
a.substring(0,4)
// -> ABCD

const b = "ABC/DEF/GHI"
b.split(/)
// -> ["ABC","DEF","GHI"]

Math.random()
: 0부터 1사이의 랜덤한 숫자 제공

Math.round()
: 소수점 아래 숫자를 반올림

Math.ceil()
: 소수점 아래 숫자를 올림

Math.floor()
: 소수점 아래 숫자를 내림

Math.Max()
: 최대값을 알려줌

Math.random()*10
// ->2.423147915...
// ->5.19437489137... 등등
// 0~10사이의 정수가 아닌 숫자를 얻을 수 있음(소수점을 가지는 float이라서!)

Math.round(1.4)
// ->1
Math.round(0.8)
// ->1
Math.round(0.1)
// ->0

Math.ceil(1.4)
// ->2
Math.ceil(0.8)
// ->1
Math.ceil(0.1)
// ->1
Math.ceil(1.01)
// ->2

Math.floor(1.4)
// ->1
Math.floor(0.8)
// ->0
Math.floor(0.1)
// ->0
Math.floor(1.01)
// ->1

// 🤫소수점 없는 10가지 랜덤수?
Math.floor(Math.random()*10)
// 여러 중복 수 최대값 찾기 = Math.max
const numbers = [1,2,6,34,7,52,2,4,6,8,3,23,1,35,52,66,72,5,6,3,10,11,12,10,66,72];
function maxnum(arr){
  console.log(Math.max(...arr));
}
maxnum(numbers);

2개의 댓글

comment-user-thumbnail
2023년 7월 19일

글이 많은 도움이 되었습니다, 감사합니다.

답글 달기
comment-user-thumbnail
2023년 7월 20일

최고최고~

답글 달기