JS(Vanilla)

배울 내용


문자열 다루기

전체코드 1


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var[x, y, z] = ['1', 2];
        document.write(typeof x, typeof y, typeof z);
        var[x, y, z] = [4, 5];
        document.write(z);

        var frustr = 'grape-melon-cherry';
        var[fruit1, fruit2, fruit3] = frustr.split('-');
        document.write(fruit1+'<br>');
        document.write(fruit2+'<br>');
        document.write(fruit3+'<br>');
        [fruit1, fruit2] = [fruit2, fruit1];
        var student = {name: 'kim', number: 5};
        var {name, number} = student;
        document.write(name+'<br>');
        document.write(number+'<br>');
        var {number, name: studentname} = student;
        document.write(name+'<br>');
        document.write(number+'<br>');

        var str = "JavaScriptava";
        document.write(str.indexOf('ava')+'<br>');
        document.write(str.indexOf('java')+'<br>');
        document.write(str.indexOf('a', 2)+'<br>');
        document.write(str.lastIndexOf('ava')+'<br>');

        document.write(str.charAt(2)+'<br>');
        document.write(str.charCodeAt(4)+'<br>');
        document.write(str.slice(3, 6)+'<br>');
        document.write(str.slice(-5, -2)+'<br>');
        document.write(str.slice(6)+'<br>');
        document.write(str.substring(5, 7)+'<br>');
        document.write(str.substr(5, 2)+'<br>');

        var str = 'javascript is fun'
        document.write(str.concat("isn't it?")+'<br>');
        document.write(str.concat("isn't it?hahaha")+'<br>');
        document.write(str+'<br>');

        var data = 'JAVA';
        document.write('우리는 ${data}를 배워요')
    </script>
</body>
</html>

JS 부분 1


var[x, y, z] = ['1', 2];
document.write(typeof x, typeof y, typeof z);
var[x, y, z] = [4, 5];
document.write(z);

var frustr = 'grape-melon-cherry';
var[fruit1, fruit2, fruit3] = frustr.split('-');
document.write(fruit1+'<br>');
document.write(fruit2+'<br>');
document.write(fruit3+'<br>');
[fruit1, fruit2] = [fruit2, fruit1];
var student = {name: 'kim', number: 5};
var {name, number} = student;
document.write(name+'<br>');
document.write(number+'<br>');
var {number, name: studentname} = student;
document.write(name+'<br>');
document.write(number+'<br>');

var str = "JavaScriptava";
document.write(str.indexOf('ava')+'<br>');
document.write(str.indexOf('java')+'<br>');
document.write(str.indexOf('a', 2)+'<br>');
document.write(str.lastIndexOf('ava')+'<br>');

document.write(str.charAt(2)+'<br>');
document.write(str.charCodeAt(4)+'<br>');
document.write(str.slice(3, 6)+'<br>');
document.write(str.slice(-5, -2)+'<br>');
document.write(str.slice(6)+'<br>');
document.write(str.substring(5, 7)+'<br>');
document.write(str.substr(5, 2)+'<br>');

var str = 'javascript is fun'
document.write(str.concat("isn't it?")+'<br>');
document.write(str.concat("isn't it?hahaha")+'<br>');
document.write(str+'<br>');

var data = 'JAVA';
document.write('우리는 ${data}를 배워요')

split()


var frustr = 'grape-melon-cherry';
var[fruit1, fruit2, fruit3] = frustr.split('-');

split()은 해당 문자열을 이용하여 문자열을 자른다. 비구조화 할당을 통해 변수에 받을 수 있다.

비구조화 할당


비구조화 할당 을 참조한다.

indexOf, lastIndexOf, slice, splice, substring, substr, concat


배열 내장 함수 를 참조한다.