JavaScript

내장함수

내장함수

  • 자바스크립트에 등록되어 있는 몇가지 내장함수들에 대해 알아봅니다.

 

 

 

eval()

  • 문자열로 표현된 자바스크립트 코드를 수행합니다.
var result = eval('2 + 2');
document.write(result);

 

 

 

isFinite()

  • 주어진 값이 유한수인지 판별합니다.
var result1 = isFinite(1000 / 3);
var result2 = isFinite(1000 / 4);
var result3 = isFinite(1000 / 0);

document.write(result1 + "<br>");
document.write(result2 + "<br>");
document.write(result3 + "<br>");

 

 

 

isNaN()

  • 값이 NaN 인지 판별합니다.
  • NaN : Not a Number의 줄임말로 '숫자가 아닌 것'을 뜻합니다.
  • 따라서 숫자가 아니면 'true', 숫자이면 'false' 값을 리턴합니다.
var result1 = isNaN("hello world");
var result2 = isNaN(1268);
var result3 = isNaN(0.0314E+2);

document.write(result1 + "<br>");
document.write(result2 + "<br>");
document.write(result3 + "<br>");

 

 

 

parseFloat()

  • 값을 분석하여 부동소수점 실수로 변환합니다.
  • 값이 분석하여 실수로 변환할 수 없으면 NaN을 리턴합니다.
var result1 = parseFloat(3.14);
var result2 = parseFloat('3.14');
var result3 = parseFloat('314e-2');
var result4 = parseFloat('0.0314E+2');
var result5 = parseFloat("hello world");

document.write(result1 + "<br>");
document.write(result2 + "<br>");
document.write(result3 + "<br>");
document.write(result4 + "<br>");
document.write(result5 + "<br>");

 

 

 

parseInt()

  • 값을 분석하여 특정 진수(radix)의 값을 반환합니다.
  • 진수는 2~36 사이의 값을 가집니다.
  • 값을 변환할 수 없으면 NaN 값을 리턴합니다.
parseInt(문자열, 해당진수);

 

  • 1111(2진수)를 10진수 값으로 바꾸면 15입니다.
parseInt("1111", 2);

 

  • 다음은 값이 기본적으로 15인 진수별 표현입니다.
  • 실수를 10진 정수로 변환하면 소수점 이하의 숫자를 버립니다.
var result1 = parseInt("0xF", 16);
var result2 = parseInt("F", 16);
var result3 = parseInt("17", 8);
var result4 = parseInt(15.99, 10);
var result5 = parseInt("Hello", 8);

document.write(result1 + "<br>");
document.write(result2 + "<br>");
document.write(result3 + "<br>");
document.write(result4 + "<br>");
document.write(result5 + "<br>");

 

 

 

encodeURI()

  • URI(Uniform Resource Identifier, 인터넷 식별자)를 utf-8로 나타내는 문자로 인코딩하여 암호화하는 함수입니다.
  • 다만 encodeURI() 함수는 다음과 같은 문자 부호를 처리하지 못합니다.
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

 

var uri = 'https://mozilla.org/?x=안녕안녕';        
var encoded = encodeURI(uri);

document.write(encoded + "<br>");

 

var uri = ';,/?:@&=+$#'; 
var encoded = encodeURI(uri);

document.write(encoded + "<br>");

 

 

 

encodeURIComponent()

  • encodeURI() 함수와 비슷한 기능을 함수입니다.
  • 다만, 알파벳, 0~9의 숫자, - _ . ! ~ * ' ( )를 제외하고 모든 문자를 이스케이프 처리합니다.

 

var uri = ';,/?:@&=+$#';
var encoded = encodeURIComponent(uri);
        
document.write(encoded + "<br>");

 

var uri = "-_.!~*'()";
var encoded = encodeURIComponent(uri);

document.write(encoded + "<br>");

 

 

 

decodeURI()

  • encodeURI() 함수로 인코딩한 URI를 해독합니다.

 

var uri = 'https://mozilla.org/?x=안녕안녕';         
var encoded = encodeURI(uri);
var decoded = decodeURI(encoded);

document.write(encoded + "<br>");
document.write(decoded + "<br>");

 

 

 

decodeURIComponent()

  • encodeURIComponent() 함수로 인코딩한 URI를 해독합니다.

 

var uri = 'https://mozilla.org/?x=안녕안녕';         
var encoded = encodeURIComponent(uri);
var decoded = decodeURIComponent(encoded);

document.write(encoded + "<br>");
document.write(decoded + "<br>");

 

 

 

댓글

댓글 본문
버전 관리
KNUT X LIKE LION
현재 버전
선택 버전
graphittie 자세히 보기