let a = 13; //정수형 변수 타입let b = 3.14; //실수형 변수 타입let c = true; //boolean 변수 타입let d = "hello"; //String 변수 타입let e = [1,2,3,4,5];
let f = {userid:"abc", userpw:"def"};
console.log(typeof(a), a);
console.log(typeof(b), b);
console.log(typeof(c), c);
console.log(typeof(d), d);
console.log(typeof(e), e);
console.log(typeof(f), f);
JavaScipt의 변수 선언 방식에는 두 가지가 존재한다. => 1. const(상수), 2. let(변수)
const형으로 변수를 선언할 경우, 한번 선언된 값은 변경될 수 없다.
let형으로 변수를 선언할 경우,, 일반 변수와 동일하게 값을 변경할 수 있다.
let number1 = 100console.log(number1);
let number2 = 100;
if(typeof(number2) !== "undefined"){
console.log(number2); //number의 값이 undefined가 아닐 경우 console출력
}
typeof메서드는 선언한 변수의 타입을 확인할 수 있는 메서드이다. => 사용방법: typeof(변수이름) => 위 예제의 경우 number2에 100이라는 정수형 숫자를 넣었기 때문에 number타입으로 출력되는 것을 확인할 수 있다.
값이 저장되지 않는 오류를 줄이기 위하여 조건문을 사용하여 사용하고자 하는 변수에 값이 할당되었는지 확인하는 과정을 수행해주면 오류를 줄일 수 있다.
Node.js 웹 서버 예제
// 1. 필요라이브러리 설치// CMD> npm install http --save// 2. 라이브러리 가져오기const http = require("http");
// 3. http변수를 이용하여 웹 서버 만들기const server = http.createServer(function (req, res){
res.writeHead(200, {"Content-type":"text/html"});
res.write("Hello World!");
res.end();
});
// 4. port설정
server.listen(3000);
// 5. 서버구동확인console.log("Web Server On!");