VScode에 있는 터미널 열기/맥북 터미널 열기
node.js파일 있는 경로로 가기
파일 경로에서 node 파일이름.확장자
으로 실행시 결과값 확인 가능
var n=10;
console.log("결과는 %d입니다",n); // printf와 같음
console.log("결과는 "+n+"입니다");
var msg="happy day~~"; //문자열
console.log("메세지: "+msg+"!!!");
console.log("메세지 %s!!!",msg);
var j={"irum":"김영환"}; //json
console.log("Json출력: %j",j);
console.log("Hello"); //터미널에서 이 파일이 있는 경로로 들어가서 "node 파일이름.확장자"로 실행 가능 / 터미널은 맥에 있는거 사용해도 되고 vscode의 터미널 사용해도 됨
console.log("안녕 노드제이에스 테스트중이야");
var n=10;
console.log("결과는 %d입니다",n); // printf와 같음
console.log("결과는 "+n+"입니다");
var msg="happy day~~"; //문자열
console.log("메세지: "+msg+"!!!");
console.log("메세지 %s!!!",msg);
var j={"irum":"김영환"}; //json
console.log("Json출력: %j",j);
var: 함수형 변수 / 재사용/업데이트/초기화 가능
let: 영역형 변수 / 변수 재선언 불가능
const: 영역형 변수 중 상수(final)
재선언가능, 업데이트 가능
var hello="안녕";
var hello="헬로우";
var cnt=5;
if(cnt<6){
var hello1="say hello~~~";
console.log(hello1);
}
console.log(hello1); //조건에 충족했을 때 값을 넣어줬기 때문에 조건 충족이 안되면 출력 안됨
console.log(hello);
결과
say hello~~~
say hello~~~
헬로우
재선언 불가능, 업데이트 가능
let hi="hi";
hi="하이라고 할게";
let times=5;
if(times>3)
{ //이 영역을 벗어나면 let 변수는 사용 불가능
let hi1="say Hi~~~";
console.log(hi1);
}
//console.log(hi1); 주석 해제시 오류 발생
console.log(hi);
주석 해제후 오류창
/Users/sunghyunchoi/Desktop/sist0615/work/es6work/varex2.js:32
console.log(hi1);
^
ReferenceError: hi1 is not defined
at Object.<anonymous> (/Users/sunghyunchoi/Desktop/sist0615/work/es6work/varex2.js:32:13)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49
Node.js v20.10.0
주석 후 결과창
say Hi~~~
하이라고 할게
업데이트 안되고, 재선언 안됨
const hi3={
message:"3th say Hi~~",
times:4
}
console.log(hi3);
//이렇게 업데이트는 안됨
// const hi3={
// message:"4th say Hi~~",
// times:5
// }
// console.log(hi3);
hi3.message="요렇게는 될까?";
hi3.times=44;
console.log(hi3);
-> 주석된 곳 주석 해제시 재정의 안된다고 메세지 뜸
결과창
{ message: '3th say Hi~~', times: 4 }
{ message: '요렇게는 될까?', times: 44 }
/*
var: 함수형 변수 / 재사용/업데이트/초기화 가능
let: 영역형 변수 / 변수 재선언 불가능
const: 영역형 변수 중 상수(final)
*/
//var Test:재선언가능, 업데이트 가능
var hello="안녕";
var hello="헬로우";
var cnt=5;
if(cnt<6){
var hello1="say hello~~~";
console.log(hello1);
}
console.log(hello1); //조건에 충족했을 때 값을 넣어줬기 때문에 조건 충족이 안되면 안됨
console.log(hello);
//let ..재선언 불가능, 업데이트 가능
let hi="hi";
hi="하이라고 할게";
let times=5;
if(times>3)
{ //이 영역을 벗어나면 let 변수는 사용 불가능
let hi1="say Hi~~~";
console.log(hi1);
}
//console.log(hi1);
console.log(hi);
//const : 업데이트 안되고, 재선언 안됨
const hi3={
message:"3th say Hi~~",
times:4
}
console.log(hi3);
//이렇게 업데이트는 안됨
// const hi3={
// message:"4th say Hi~~",
// times:5
// }
// console.log(hi3);
hi3.message="요렇게는 될까?";
hi3.times=44;
console.log(hi3);
var a=1;
let b=2;
function myFunction(){
var a=3;
let b=4;
if(true)
{
var a=5;
let b=6;
console.log(a); //5
console.log(b); //6
}
console.log(a); //5
console.log(b); //4
}
myFunction();
console.log(a); //1
console.log(b); //2
5
6
5
4
1
2
function func1(x,y=200,z=300)
{
console.log(x,y,z);
}
func1(3,4,5);
func1(100); //y,z는 이미 정의되어 있어서 x값만 들어가서 사용 된 거임
func1(3,4); //x,y값을 지정해주고 z는 인자에 300으로 정의해놔서 사용 됨
func1(); //x값만 undefined나고 뒤는 나옴
결과값
3 4 5
100 200 300
3 4 300
undefined 200 300
function func2(a,b) {
return a+b;
}
console.log(func2(5,6));
결과값
11
let arr=[3,5,9];
let r2=func2(arr[0],arr[1]);
console.log(r2); //8
결과값
8
... :es6에서 추가된 기능...펼침연산자
let arr=[3,5,9];
func1(...arr); //배열 안에 값들을 다 불러줌
func1(arr[0],arr[1],arr[2]); //...arr과 같음
let r3=func2(...arr); //함수인자 갯수와 배열갯수가 맞는 것 까지만 나옴
console.log(r3);
결과값
3 5 9
3 5 9
8
function func1(x,y=200,z=300)
{
console.log(x,y,z);
}
func1(3,4,5);
func1(100); //y,z는 이미 정의되어 있어서 x값만 들어가서 사용 된 거임
func1(3,4); //x,y값을 지정해주고 z는 인자에 300으로 정의해놔서 사용 됨
func1(); //x값만 undefined나고 뒤는 나옴
function func2(a,b) {
return a+b;
}
console.log(func2(5,6));
let arr=[3,5,9];
let r2=func2(arr[0],arr[1]);
console.log(r2); //8
//... :es6에서 추가된 기능...펼침연산자
func1(...arr); //배열 안에 값들을 다 불러줌
func1(arr[0],arr[1],arr[2]); //...arr과 같음
let r3=func2(...arr); //함수인자 갯수와 배열갯수가 맞는 것 까지만 나옴
console.log(r3);
let arr1=[2,4,6];
let arr2=[6,7];
let arr3=[11,12,...arr1,22,...arr2];
console.log(arr3.length);
console.dir(arr3); //구조까지 나오는 것
let arr4=[...[100,200],...arr2];
console.log(arr4);
결과값
8
[
11, 12, 2, 4,
6, 22, 6, 7
]
[ 100, 200, 6, 7 ]
function fsum(arr) {
let sum=0;
//방법1
/*for(i=0;i<arr.length;i++)
{
sum+=arr[i];
}*/
//방법2
for(let a in arr)
{
sum+=arr[a];
}
console.log("합계: "+sum);
}
fsum(arr3);
결과창
합계: 70
function func3(a,b,c,d,e) {
console.log(a,b,c,d,e);
}
//func3(arr3); undefined 나옴
func3(...arr3); //펼침연산자를 통해 펼쳐서 나옴
결과창
11 12 2 4 6
let arr1=[2,4,6];
let arr2=[6,7];
let arr3=[11,12,...arr1,22,...arr2];
console.log(arr3.length);
console.dir(arr3); //구조까지 나오는 것
let arr4=[...[100,200],...arr2];
console.log(arr4);
function fsum(arr) {
let sum=0;
//방법1
/*for(i=0;i<arr.length;i++)
{
sum+=arr[i];
}*/
//방법2
for(let a in arr)
{
sum+=arr[a];
}
console.log("합계: "+sum);
}
fsum(arr3);
function func3(a,b,c,d,e) {
console.log(a,b,c,d,e);
}
//func3(arr3); undefined 나옴
func3(...arr3); //펼침연산자를 통해 펼쳐서 나옴
화살표함수: new 객체 생성 안됨
자바의 Ramda익명함수와 같은 방식(function키워드 대신 화살표사용)
ex)
function 함수명(){
함수로직
}
function (){
함수로직
}
const 변수=function (){
함수로직
}
const 변수= ()=>{
함수로직
}
function f1(){
console.log("일반함수 f1");
}
f1();
결과창
일반함수 f1
//화살표
let f2=()=>console.log("화살표 함수 f2"); //한줄 짜리 코드면 {}생략 가능
f2();
결과창
화살표 함수 f2
//일반
function f3(a,b) {
return a+b;
}
let a=f3(5,7);
console.log(a);
결과
12
//화살표
let f4=(a,b)=>a+b; //한줄일땐 리턴 생략 가능
let b=f4(11,22);
console.log(b);
결과창
33
//일반
function f5(x,y,z=100) {
return x+y+z;
}
console.log(f5(1,2,3));
console.log(f5(40,50));
결과창
6
190
-> 2번째 결과는 인자값에 기본값을 주어서 x,y값만 대입 되고 z는 기본값으로 대입 되서 나오기 때문에 190
//f5를 화살표함수 f6으로 구현 후 호출
let f6=(x,y,z)=>x+y+z;
console.log(f6(4,5,6));
결과값
15
//화살표함수: new 객체 생성 안됨
//자바의 Ramda익명함수와 같은 방식(function키워드 대신 화살표사용)
/*
function 함수명(){
함수로직
}
function (){
함수로직
}
const 변수=function (){
함수로직
}
const 변수= ()=>{
함수로직
}
*/
function f1(){
console.log("일반함수 f1");
}
f1();
//화살표
let f2=()=>console.log("화살표 함수 f2"); //한줄 짜리 코드면 {}생략 가능
f2();
//일반
function f3(a,b) {
return a+b;
}
let a=f3(5,7);
console.log(a);
//화살표
let f4=(a,b)=>a+b; //한줄일땐 리턴 생략 가능
let b=f4(11,22);
console.log(b);
//일반
function f5(x,y,z=100) {
return x+y+z;
}
console.log(f5(1,2,3));
console.log(f5(40,50));
//f5를 화살표함수 f6으로 구현 후 호출
let f6=(x,y,z)=>x+y+z;
console.log(f6(4,5,6));
let irum="최성현";
let birth=1995;
let likefood="삼겹살";
let curYear=new Date().getFullYear(); //숫자 4자리 반환
//예전
let result="이름: "+irum+"\n태어난 연도: "+birth+"\n나이: "+(curYear-birth)+"\n좋아하는 음식: "+likefood;
console.log(result);
결과값
이름: 최성현
태어난 연도: 1995
나이: 28
좋아하는 음식: 삼겹살
중간변수 &{변수명}
``안에서 값 받아올 때 &{변수명}사용
let irum="최성현";
let birth=1995;
let likefood="삼겹살";
let curYear=new Date().getFullYear(); //숫자 4자리 반환
//리터럴(백틱 ``)...중간변수 &{변수명}
let result2=`이름: ${irum}
태어난연도: ${birth}
나이: ${curYear-birth}
최애음식: ${likefood}`;
console.log(result2);
결과값
이름: 최성현
태어난연도: 1995
나이: 28
최애음식: 삼겹살
Map도 똑같음
//es6 좀 더 언어적으로 바뀜, Set,Map 가능(컬렉션)
let set=new Set("abcccdddeeefffff"); //중복문자 걸러줌
console.log(set);
출력값
Set(6) { 'a', 'b', 'c', 'd', 'e', 'f' }
let set=new Set("abcccdddeeefffff"); //중복문자 걸러줌
set.add("g");//없으니까 추가됨
console.log(set);
출력값
Set(7) { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }
있는지 없는지 반환하는데 있으면 true 없으면 false
let set=new Set("abcccdddeeefffff");
console.log(set.has("a")); //has 있는지 없는지 반환하는데 있으면 true 없으면 false
console.log(set.has("z"));
결과값
true
false
console.log("길이: "+set.size);
결과값
길이: 7
console.log(...set);
결과값
a b c d e f g
set.delete("a"); // 하나씩 삭제/ a만 삭제
console.log(...set);
결과값
b c d e f g
set.clear(); //전체삭제
console.log(set.size);
결과값
0
let a=6;
let b="6";
console.log(a==b); //값으로만 비교해서 true 반환
console.log(a===b); //타입까지 비교해서 false 반환
결과값
true
false
//es6 좀 더 언어적으로 바뀜, Set,Map 가능(컬렉션)
let set=new Set("abcccdddeeefffff"); //중복문자 걸러줌
console.log(set);
set.add("g");//없으니까 추가됨
console.log(set);
console.log(set.has("a")); //has 있는지 없는지 반환하는데 있으면 true 없으면 false
console.log(set.has("z"));
console.log("길이: "+set.size);
console.log(...set);
set.delete("a"); // 하나씩 삭제/ a만 삭제
console.log(...set);
set.clear(); //전체삭제
console.log(set.size);
//비교연산자
let a=6;
let b="6";
console.log(a==b); //값으로만 비교해서 true 반환
console.log(a===b); //타입까지 비교해서 false 반환
es6 에서는 class 기능이 추가됨
//es6 에서는 class 기능이 추가됨
class Student{
//생성자...무조건 클래스명이 constructor
constructor(name){
this.name=name;
}
//멤버함수 방법1
/*showName(){
console.log("내 이름은 "+this.name+"입니다");
}*/
//리터럴 방법2
showName(){
console.log(`내 이름은 ${this.name} 이랍니다!!!`);
}
}
let s1=new Student("민규");
console.log("이름: "+s1.name);
s1.showName();
let s2=new Student("성경");
console.log(`이름: ${s2.name}`);
s2.showName();
출력창
이름: 민규
내 이름은 민규 이랍니다!!!
이름: 성경
내 이름은 성경 이랍니다!!!