JavaScript

데이터타입의 프로퍼티와 메서드

paintover23 2023. 6. 28. 15:46
728x90

[문자열의 프로퍼티와 메서드]

var str1="hello world";
str1.length; <--문자열 길이
str1.charAt(0); <--문자 H가 추출될 것
str1.split(""); <--공백 기준으로 문자 나눈 후 배열 [hello,world]로 출력될 것

[예제]
var str1 = 'Hello World';
document.write(str1.length); <--11로 출력
document.write(str1.charAt(0)); <--H출력
document.write(str1.split(" ")); <-- Hello,World 출력

[배열의 프로퍼티와 메서드]

var.fruit=["사과","배","포도"];
fruit.length; <--데이터개수(3개)
fruit.push("딸기"); <--배열 뒤에 데이터 삽입(포도 뒤에 딸기 삽입)
fruit.unshift("레몬"); <--배열 앞에 데이터 삽입(사과 앞에 레몬 삽입)
fruit.pop(  );<--배열 뒤의 데이터 제거 (딸기 없어짐)
fruit.shift(  ); <--배열 앞의 데이터 제거 (레몬 없어짐)

[예제]
var fruit=["Apple","Banana","Tomato"];
document.write(fruit.length);
fruit.push("A"); <--Apple,Banana,Tomato
fruit.unshift("B");

document.write(fruit); <-- 3B,Apple,Banana,Tomato,A

fruit.pop();
fruit.shift();
document.write(fruit); <-- 3B,Apple,Banana,Tomato,AApple,Banana,Tomato

[math의 수학 연산 메서드]

math abs(-3); <-- 절대값(3)
math.ceil(0.3); <--올림 (1)
math.floor(10.9); <--내림 (10)
math.random(  ); <--임의의 숫자출력

[예제]
document.write(Math.abs(-3));
document.write(Math.ceil(0.3));
document.write(Math.floor(10.9));
document.write(Math.random());
<-- 31100.661951881149504

[문자를 숫자로 변환하는 메서드]

parseInt("20.6"); <--정수 형태의 20 변환(20)
parseFloat("20.6"); <--실수 형태의 20.6 변환(20.6)

[예제]
var str1="20.14";
document.write(parseInt(str1)); <--20 출력
document.write(parseFloat(str1)); <--2020.14 출력
728x90
반응형

'JavaScript' 카테고리의 다른 글

반복문  (0) 2023.06.28
조건문  (0) 2023.06.28
연산자  (0) 2023.06.28
자바스크립트 데이터타입  (0) 2023.06.28
자바스크립트 변수 선언  (0) 2023.06.28