<script></script> 태그 안에 작성<script src="./testjs.js"></script>10 + 2010+20;;을 사용하여 종결을 나타냄break case if etc,,,| 단독 | 다른 식별자와 함께 | |
|---|---|---|
| ( ) 없음 | 변수 input | 속성 Array.length |
| 식별자 뒤 ( ) | 함수 addNum(1,3) | 메서드 Math.abs(-273) |
/**/ , //
- 리터럴(literal) : 값을 생성하기 위해 미리 약속한 표기법 (코드에서 값을 나타내는 표기법)
- 문자 리터럴
- 숫자 리터럴
- boolean 리터럴
- 문(statement) : 코드에서 최소 실행 단위
- 반목문
- 조건문
- 할당문
name = ‘길동’;- 표현식 : 값으로 평가될 수 있는 문
- 숫자 표현식 :
1- 연산자 표현식 :
1+1- 함수 호출 표현식 :
call( )
alert() : 경고 창 console.log() : 개발자 도구 console에 출력됨console.log(typeof 변수명)let num = 1;
1. 정수
2. 실수
3. 2진수 8진수 16진수
InfinityNaNlet name = ‘길동’;
${}을 사용하면 변수와 문자열을 함께 사용할 수 있음 `${변수명}\`/nlet isFree = ‘false’;
!!을 붙이면 boolean 타입으로 변경할 수 있음 console.log(!!0);정해진 값이 비어있음
정의 x, 할당 x
데이터 처리를 위한 연산자의 종류
+ - * / % **
+(양) -(음) !(부정)
console.log(+true); //1
console.log(+false); //0
console.log(+null); //0
console.log(+undefined); //NaN
console.log(+'text'); //NaN
console.log(!!'text'); //true
console.log(!!''); //false+= -= *= %=
++ --
> < >= <=
! && ||
& ^ ~ << >>
== != 값이 다른가=== 값과 타입까지 같은가!== 값과 타입이 다른가값을 저장할 때 사용하는 식별자
let name = ‘홍길동’;const MAX_FRIST = 100; : 변수명을 대문자로 선언, 변경 안될 값 넣기
- 카멜케이스 형식
userName- 파스칼케이스
PascalCase: 클래스명 작성하거나 생성자 함수 만들때- 스네이크 케이스
MAX_SIZE: 상수
if (조건문) {실행문}
if (조건식) {실행문} else {실행문}
if (조건식) {실행문} else if (조건문) {실행문} else {실행문}
조건식 ? 참일경우 : 거짓일 경우
switch () {
case :
break;
case :
break;
default: ;
}
for (let i = 0 초기식 ; i < arr.length 조건식; i++) {실행문}
배열과 객체 데이터를 다룰 때 많이 사용됨
let arr = [10, 20, 30, 40, 50];
for (let i = 0; i < arr.length; i++) {
console.log('for' + arr[i]);
}
for (let value of arr) {
console.log('of' + value);
}for (let ind in arr) {
console.log('in' + ind);
console.log(arr[ind]); // 값이 나온다
}//while
let index = 0;
while (index < arr.length) {
console.log('while' + arr[index]);
index++;
}
do while 반복문
//do-while
index = 0;
do {
console.log('do-while' + arr[index]);
index++;
} while (index < arr.length);
function 함수이름(매개변수) {실행}
function add(a, b) {
console.log(a + b);
}
let 변수명 = function(매개변수) {실행}
let add2 = function (a, b) {
console.log(a + b);
};
let 함수이름 = (매개변수) ⇒ 실행;
let add3 = (a, b) => a + b;
+) 할당
let add3 = add2;
new Date()
(function run(){})
함수 내부에서 함수를 실행하는 것
return undefined; : 기본return; : 실행 종료key : 문자, ‘문자열’, 숫자
value : 원시값, 객체 (배열, 함수 등도 포함)
속성 : 객체에 있는 값
메서드 : 함수가 속성일 경우
접근 방식
객체명['키']function을 만들어서 추가할 수 있음
function addKey(obj, key, value) {
obj[key] = value;
}
key:value 같으면 짧게 줄일 수 있음
const x = 0;
const y = 0;
const coord = { x: x, y: y };
number 타입에서 사용하는 내장 객체
let num2 = 123456789.1234;
console.log(num2.toFixed());
console.log(num2.toString());
console.log(num2.toLocaleString());
console.log(num2.toPrecision(2));
console.log(Math.PI);
console.log(Math.abs(-10));
string에서 사용하는 내장 객체
let str = ' abc defg ';
console.log(str.length); //str의 길이
console.log(str.charAt(0)); //해당 인덱스의 값 찾기
console.log(str.indexOf('g')); //인덱스 찾기
console.log(str.lastIndexOf('g'));
console.log(str.includes('c')); //해당 값 유무
console.log(str.startsWith('a')); //시작 값인지
console.log(str.endsWith('g')); //끝나는 값인지
console.log(str.slice(5)); //인덱스 다음 부터
console.log(str.trim()); //앞뒤로 공백 제거
let long = 'hello everyone~! today is 12 march';
console.log(long.split(' ')); // 공백 기준 split
console.log(long.split('!', 2)); // ! 기준 split
console.log(new Date().getDate());
arr = Array.of(1, 2, 3, 4);const arr2 = Array.from(arr);let arr3 = arr;let arr4 = Array.from('hi my name');arr4 = Array.from({
0: 'hi',
1: 'everyone',
length: 2,
});
console.log(arr4);.push() : 맨뒤에 값 추가.unshift() : 맨앞에 값 추가.pop() : 맨뒤 값 삭제.shift() : 맨앞 값 삭제.splice(i,n) : index i부터 n개 삭제, 중간 값 삭제arr1.concat(arr2) : arr1 뒤에 arr2가 병합됨.flat().fill().join()forEach() : 배열의 각 요소에 대해 호출