var count = 1;
var PI = 3.14;
typeof "hello"; // string
typeof 123; // number
typeof null; // object (JS의 오래된 버그)
var arr = [1,2,3];
arr.push(4); // [1,2,3,4]
arr.pop(); // [1,2,3]
arr.shift(); // [2,3]
arr.unshift(0);// [0,2,3]
var arr = [1,2,3,4];
var result = arr.filter(function(x){ return x !== 2; }); // [1,3,4]
💡 실무 예제 사용자 권한 목록에서 특정 권한 제거하기
var permissions = ["READ", "WRITE", "DELETE"];
var safePermissions = permissions.filter(function(p){ return p !== "DELETE"; });
// ["READ", "WRITE"]
var arr = [1,1,2,2,3];
var unique = [];
for(var i=0;i<arr.length;i++){
if(unique.indexOf(arr[i]) === -1){
unique.push(arr[i]);
}
}
// [1,2,3]
var a = [1,2];
var b = [3,4];
var merged = a.concat(b); // [1,2,3,4]
var sum = [1,2,3,4].reduce(function(acc,cur){ return acc+cur; }, 0); // 10
"hello world".indexOf("world"); // 6
"hello world".indexOf("hello") !== -1; // true
"hello world".replace("world", "JS"); // hello JS
💡 실무 예제 사용자 입력에서 불필요한 공백 제거 후 금칙어 필터링
var input = " bad word ";
input = input.replace(/^\s+|\s+$/g,"").replace("bad","***"); // "*** word"
"apple,banana,cherry".split(","); // ["apple","banana","cherry"]
"javascript".slice(0,4); // "java"
function add(a,b){ return a+b; }
var sub = function(a,b){ return a-b; };
💡 실무 예제 콜백 함수 전달
var arr = [1,2,3];
arr.forEach(function(x){ console.log(x*2); });
var mul = function(a,b){ return a*b; };
var user = {name:"Tom", age:20};
user.name; // Tom
user["age"]; // 20
💡 실무 예제 API 응답 데이터 파싱
var response = {id:1, profile:{name:"Alice"}};
var name = (response.profile && response.profile.name) ? response.profile.name : "Guest";
if(x > 10) {...} else {...}
x > 10 ? "크다" : "작다";
for(var i=0;i<5;i++) console.log(i);
for(var i=0;i<[1,2,3].length;i++) console.log([1,2,3][i]);
[1,2,3].forEach(function(x){ console.log(x); });
setTimeout(function(){ console.log("done"); }, 1000);
function getUser(callback){
var xhr = new XMLHttpRequest();
xhr.open("GET","/api/user",true);
xhr.onreadystatechange = function(){
if(xhr.readyState===4){
if(xhr.status===200){
callback(null, JSON.parse(xhr.responseText));
}else{
callback("에러 발생");
}
}
};
xhr.send();
}
getUser(function(err,user){
if(err) console.error(err);
else console.log(user);
});
var result = [1,2,3,4,5]
.filter(function(x){ return x%2===0; })
.map(function(x){ return x*x; })
.reduce(function(a,b){ return a+b; },0); // 20
ES5에서는 최신 문법이 없으므로 가독성은 다소 떨어지지만, 콜백 패턴과 기본 배열/문자열 메서드만으로도 충분히 실무 개발이 가능합니다.