๋ ผ๋ฆฌ์ฐ์ฐ์ NOT
๋ ผ๋ฆฌ ๋ถ์ (!) ์ฐ์ฐ์๋ ์ธ์ ๋ ๋ถ๋ฆฌ์ธ ๊ฐ์ ๋ฐํํ๋ค. ๋จ, ํผ์ฐ์ฐ์๊ฐ ๋ฐ๋์ ๋ถ๋ฆฌ์ธ ๊ฐ์ผ ํ์๋ ์๋ค.
๋ง์ฝ ํผ์ฐ์ฐ์๊ฐ ๋ถ๋ฆฌ์ธ ๊ฐ์ด ์๋๋ฉด ๋ถ๋ฆฌ์ธ ํ์ ์ผ๋ก ์๋ฌต์ ํ์ ๋ณํ๋๋ค.
!false // true
!(3>2) // false
!undefined // true (undefined๋ฅผ false๋ก ์ทจ๊ธ-falsy)
!"hello" // false ("hello"๋ฅผ true๋ก ์ทจ๊ธ-truthy)
!0; // true
๊ธฐ์ตํด์ผ ํ 9๊ฐ์ง falsy ๊ฐ
if ๋ฌธ์์ false๋ก ๋ณํ๋๊ธฐ ๋๋ฌธ์, if ๊ตฌ๋ฌธ์ด ์คํ๋์ง ์์ต๋๋ค.
if (false)
if (null)
if (undefinded)
if (0)
if (-0)
if (NaN)
if ('')
if ("")
if (``)
์ ์ธ๊ณผ ํธ์ถ
์ ์ธ // num1, num2 => ๋งค๊ฐ๋ณ์ = parameter = ์ธ์
function handleplus (num1, num2) {
num1 + num2;
}
ํธ์ถ // 40,70 => argument = ๊ฐ (value)= ์ธ์=์ ๋ฌ์ธ์
handlePlus(40,70);
๋ฌธ์์ด ํฉ์น๋ 2๊ฐ์ง ๋ฐฉ๋ฒ
1. "+" ์ฐ์ฐ์
const str1 = "Javascript";
const str2 = "Developer";
console.log(str1 + str2); // "Javascript Developer"
2. concat() ํจ์
const str1 = "Javascript";
const str2 = "Developer";
console.log(str1.concat(str2)); // "Javascript Developer"
์์์ ํน์ ๋ฌธ์ ์์น ์ฐพ๊ธฐ (indexOf ํจ์)
str.indexOf(๊ฒ์ํ ๊ฐ, ์์์์น) - ์์์ ๊ฒ์ํ ๊ฐ์ ์ฐพ๊ธฐ ์์
const str = "Happy Day";
console.log(str.indexOf('Happy')); // 0
console.log(str.indexOf('happy')); // -1 (์ผ์นํ๋ ๊ฒฐ๊ณผ๊ฐ์ด ์์ ๊ฒฝ์ฐ์๋ -1๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด)
console.log(str.indexOf('Day')); // 6
console.log("Happy Day Day".indexOf('Day')); // 6
console.log(str.indexOf('a')); // 1
console.log(str.indexOf('a',5)); // 7 ๋ป:"a"์ด๋ผ๋ ๋ฌธ์๋ฅผ ์ฐพ๋๋ฐ 5 ์ดํ๋ถํฐ ๊ทธ ๊ฐ์ ์ฐพ์๋ผ.
๋ค์์ ํน์ ๋ฌธ์ ์์น ์ฐพ๊ธฐ (lastIndexOf)
lastIndexOf(๊ฒ์ํ ๊ฐ , ์์์์น) - ๋์์๋ถํฐ ๊ฒ์ํ ๊ฐ์ ์ฐพ๊ธฐ ์์
const str = "Happy Day";
console.log(str.lastIndexOf('a')); // 7
//๋ป:๊ฒฐ๊ณผ๊ฐ์ ๋ค์์๋ถํฐ ๋ช๋ฒ์งธ ์์น๋ผ๋ ๊ฒ์ ๋งํด์ฃผ๋ ๊ฒ์ด
์๋๋ผ ๋งจ ์์์๋ถํฐ ์์น๋ฅผ ๋ฐํํด ์ฃผ๊ธฐ ๋๋ฌธ์ 7์ ๋ฐํํ๊ฒ ๋ฉ๋๋ค.
์ฆ, ๊ฐ์ ๋ค์์๋ถํฐ ์ฐพ๊ธฐ ์์ํ์ง๋ง ๊ฒฐ๊ณผ๊ฐ์ ๋์ผํ๊ฒ ๋งจ ์์์๋ถํฐ ์์น์ ๊ฐ์ ๋ฐํํ๊ฒ ๋๋ ๊ฒ์
๋๋ค.
console.log(str.lastIndexOf('a',5)); // 2
//๋ป:"lastIndexOf๋ผ๊ณ ํด์ ์ฐพ์ ๊ฐ์ 'a'์ ์ฐพ๋๋ฐ ๋ค์์๋ถํฐ 5๋ฒ์งธ ์์น์์๋ถํฐ
์์ผ๋ก ์ฐพ๊ธฐ ์์ํ๋ผ๋ ๊ฒ์
๋๋ค.
string.substring(start, end) // end ์๊น์ง๋ง ์ถ๋ ฅ
const str = '์๋ฐ์คํฌ๋ฆฝํธJS';
const result1 = str.substring(0, 2); // ๊ฒฐ๊ณผ : "์๋ฐ"
const result2 = str.substring(2, 5); // ๊ฒฐ๊ณผ : "์คํฌ๋ฆฝ"
const result3 = str.substring(2, 6); // ๊ฒฐ๊ณผ : "์คํฌ๋ฆฝํธ"
const result4 = str.substring(2); // ๊ฒฐ๊ณผ : "์คํฌ๋ฆฝํธJS"
///์์ ์ผ๋///
const str = '์๋ฐ์คํฌ๋ฆฝํธJS';
const result1 = str.substring(-4, 5); // str.substring(0, 5) // ๊ฒฐ๊ณผ : "์๋ฐ์คํฌ๋ฆฝ"
const result2 = str.substring(2, -1); // str.substring(0, 2) // ๊ฒฐ๊ณผ : "์๋ฐ"
string.slice(start, end) // substring(start, end) ๊ฐ๊ณผ ๊ฐ๋ค.
๋จ, ์์๋ก ๋์ฌ์,
slice(start, end)๋ string์ ๊ฐ์ฅ ๋ค์์ ์์๋งํผ ๋ด๋ ค์จ index๋ก ์ทจ๊ธํ๋ค.
const str ='abcde';
console.log(str.slice(-3); // 'cde'
.spilt()
CSV(์์ด: comma-separated values)
์ํฐํค๋ก ๊ตฌ๋ถํ๊ณ ์ถ์ผ๋ฉด csv.split("\n");
immutable ์ด๋?
์๋ณธ์ด ๋ณํ์ง ์๋๋ค.
๋ชจ๋ string method๋ immuatble์ด๋ค!!
array method๋ immutable์ธ์ง mutable์ธ์ง ์ ํ์ธํด์ผํ๋ค.
property & method ์ฐจ์ด
Object in JavaScript is just key-value pairs stored in a Hash. The difference between property and method is that -- property is a value stored in the hash key, whereas method is a function stored in hash key.
var person = {
name: "John Doe",
sayHello: function() {
console.log("hello");
}
}
In this code sample.
- name is the property of object person, it stored String "John Doe", you can access it via dot notation person.name.
- sayHello is the method of object, and it is a function. You can also access it using dot notation person.sayHello(). Notice that sayHello is just a function, and function invocation requires you to use () here.
object.something; - it is object with propery;
object.something(); is object method;
์ถ์ฒ: https://teamtreehouse.com/community/difference-between-object-methods-and-object-properies