1) Primitive Types
Mac 기준 크롬창에서 상단의 <보기> -> 개발자정보 -> 자바스크립트 콘솔 이용
BASIC SYNTAX
5-1) const
const hens = 4;
hens = 20; // ERROR !!
const age = 17;
age = age + 1 ; // ERROR !!
5-2) var
Variables can Change Types
let firstName = "Tony"; -> Double quotes work
let msg = "plz do not fee the chimps!";
let animal = 'Dumbo Octopus'; -> So do single quotes
let bad = "this is worng'; -> This DOES NOT work !
C H I C K E N
0 1 2 3 4 5 6
thing.method( )
2-1) uppercase, lowercase
2-2) trim
연이은 메소드도 가능.
thing.method(arg)
some methods accept argument that modify their behavior.
let tvShow = 'catdong';
tvShow.indexOf('cat'); // 0
tvShow.indexOf('dog'); // 3
tvShow.indexOf('z'); // -1 (not found)
3-1) slice
3-2) replace
3-3) repeat
개정 전
개정 후
- Undefined
-Null
-console
-alert
-prompt
MAKING DECISIONS WITH CODE
let random = Math.random();
if (random < 0.5) {
console.log("your number is less than 0.5 !!!")
console.log(random);
}
const dayOfWeek = "Saturday";
if (dayOfWeek === 'Monday') {
console.log(' I hate monday !')
} else if (dayOfWeek === 'Saturday' ) {
console.log("ya ! i love saturday")
}
const age = 8;
if (age <= 5) {
console.log("you are a child")
} else if (age <10) {
console.log("you pay $10")
} else if (age < 65) {
console.log("you pay $20")
}
If nothing else was true, do this...
const dayOfWeek = 'Monday';
if(dayOfWeek === 'Monday') {
console.log("Today Monday!!")
} else if (dayOfWeek === 'Saturday') {
console.log("Today Saturday!!")
} else if (dayOfWeek === 'Friday') {
consolee.log("Today Friday !!")
} else {
console.log("UGH!!")
}
const dayOfWeek = prompt('Entera day').toLowerCase();
if(dayOfWeek === 'MOnday') {
console.log("Today Monday!!")
} else if (dayOfWeek === 'saturday') {
console.log("Today Saturday!!")
} else if (dayOfWeek === 'friday') {
consolee.log("Today Friday!!!")
} else {
console.log("UGH!!")
}
const passsword = prompt("Please enter a new password");
//password must be 6+ characters
// password cannot include space
if (passsword.length >=6) {
console.log("LONG ENOUGH PASSWORD")
} else {
console.log("PASSWORD TOO SHORT ! Must be 6+ Characters!!")
}
if (passsword.indexOf(' ') === -1) {
console.log("GOOD JOB NO SPACE!!")
} else {
console.log("REMOVE THE SPACE !!")
}
< Nested If 문>
const passsword = prompt("Please enter a new password");
//password must be 6+ characters
// password cannot include space
if (passsword.length >6) {
if (passsword.indexOf(' ') === -1) {
console.log("VALID PASSWORD!!")
} else {
console.log("REMOVE THE SPACE!!")
}
} else {
console.log("PASSWORD TOO SHORT !! MUST be 6+ !!")
}
Falsy values
- false
- 0
- ""(empty string)
- null
- undefined
- NaN
Everything else is truthy !
Logical Operators (combining expresions)
const password = prompt("Enter your password")
if (password.length >= 6 && password.indexOf(' ')=== -1) {
console,log("VALID PASSWORD")
} else {
console.log("INVALID PASSWORD!!")
}
const age = 90;
if(age < 5 || age >= 65 ) {
console.log("FREE")
}else if (age<10) {
console.log("$10")
}else if (age < 65) {
console,log("$20")
}
const age = 90;
if(age >= 65 || (age >=0 && age <5)){
console.log("FREE")
}else if (age >= 5 && age <10) {
console.log("$10")
}else if (age>=10 && age < 65) {
console,log("$20")
}
expression returns true if expression is false
const age = 45;
if (!(age>=0 && age<5 || age>=65)) {
console.log("YOU ARE NOT FREE !!")
}
The switch statement is another control-flow statement that can replace multiple if statemetns.
const day = 4;
switch(day){
case 1:
console.log("HELLO");
break;
case 2 :
console.log("MONDAY");
break;
case 3:
console.log("HELLO");
break;
case 4 :
console.log("MONDAY");
break;
case 5:
console.log("HELLO");
break;
case 6 :
console.log("MONDAY");
break;
default:
console.log("I DKN");
}