let str_1 = '123Hello';
let str_2 = '12.3Hello';
console.log(parseInt(str_1)); // only the number is extracted(123)
console.log(parseFloat(str_2)); // only the number is extracted(12.3)
console.log('1' === 1); // expected output: false
console.log('1' == 1): // expected output : true
const object1 = {
name: "hello"
}
const object2 = {
name: "hello"
}
console.log(object1 === object2); // false
console.log(object1 === object1); // true
console.log(-11 % 3); // output: -2
console.log(!!'0') //
var arr = [1, 2, 3, 4, 5];
for (var index in arr) {
console.log(index);
// 배열의 인덱스(0, 1, 2, 3, 4) 출력
}
var arr = [1, 2, 3, 4, 5];
for (var index of arr) {
console.log(index);
// 배열의 값(1, 2, 3, 4, 5) 순차적으로 출력
}
자바스크립트에도 배열의 개념이 존재. 단, 타 언어에서와의 차이점은 자바스크립트 배열은 여러 타입의 변수를 한꺼번에 가지고 있을 수 있음(배열의 배열값을 가지는 것도 가능)
var arr = [1, 2, 0.3, 4.1, "5", [1,2,3]];
console.log(arr); // [1, 2, 0.3, 4.1, "5", Array(3)]
배열에서 값을 맨 마지막에 추가하고자 할 적에는 .push(x), 맨 처음에 추가하고자 할 적에는 unshift(x) 명령어를 활용(반대로 맨 마지막 원소를 제거하고자 할 적에는 .pop(), 맨 처음 원소를 제거하고자 할 적에는 .shift() 명령어를 활용)
splice(start idx, count, (option)): Add & Remove element(s)
var numbers = [1, 2, 3, 4, 5];
// Removes 2 elements starting from index 0 and adds 2 new elements
numbers.splice(0, 2, "one", "two");
console.log(numbers);
var names = ["John", "Jack"];
//Inserts element(s) at position index
names.splice(0, 0, "Jill", "Jim", "Jacob");
console.log(names);
//removes element(s) at position index, 0 is the index and 2 is the number of items removed
names.splice(0, 2);
console.log(names);
// MethodsForConditionsExample.js
var arr1 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
var arr2 = [10, 12, 14, 16, 18, 20];
function isEvenNumber(number) {
return number % 2 == 0;
}
// arrayname.every()
console.log(arr1.every(isEvenNumber));
console.log(arr2.every(isEvenNumber));
// arrayname.some()
console.log(arr1.some(isEvenNumber));
console.log(arr2.some(isEvenNumber));
// MethodsForTransformationExample.js
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function isEvenNumber(number) {
return number % 2 == 0;
}
// arrayname.filter(): 매개조건에 맞는 새로운 배열을 return
console.log(arr1.filter(isEvenNumber)); // Extract the even numbers from the array
// arrayname.reduce(): 0(initial)부터 시작하여 function에 맞는 값을 return
var sum = arr1.reduce(function (sum, number) { // Calculates the sum of the numbers
return sum + number;
}, 0);
console.log(sum) // 55
// arrayname.map(): 주어진 배열과 동일한 사이트이면서 function 조건을 만족시키는 새로운 배열을 return
var squares = arr1.map(function (number) { // Calculates the squares of the numbers
return number * number;
});
console.log(squares); // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
// Print the elements of an array with their index
var numbers = ['One', 'Two', 'Three', 'Four', 'Five'];
numbers.forEach(function(item, index) {
console.log('Item at index ' + index + ' has the value ' + item);
});
// "Item at index 0 has the value One"
// "Item at index 1 has the value Two"
// "Item at index 2 has the value Three"
// "Item at index 3 has the value Four"
// "Item at index 4 has the value Five"
Java, C++
int test;
test = 0;
function catName(name) {
console.log("My cat's name is " + name);
}
catName("Tigger");
// 위 코드의 결과는: "My cat's name is Tigger"
catName("Chloe");
function catName(name) {
console.log("My cat's name is " + name);
}
// 위 코드의 결과는: "My cat's name is Chloe"
num = 0;
num += 7;
console.log(num);
// 에러없이 실행되는 코드
var a = 2;
var b = a;
b++;
console.log(a); // a = 2
console.log(b); // b = 3 (값-복사)
var a = [1,2,3,4];
var b = a;
a.push(5);
console.log(a); // a = [1,2,3,4,5]
console.log(b); // b = [1,2,3,4,5]
var a = [1,2,3,4];
var b = a;
b = [5,6,7];
a.push(5);
console.log(a); // a = [1,2,3,4,5]
console.log(b); // b = [5,6,7]
function foo(x) {
x.push(5);
console.log(x); // x = [1,2,3,5]
x = [5,6,7];
x.push(8);
console.log(x); // x = [5,6,7,8]
}
var a = [1,2,3];
foo(a);
console.log(a); // a = [1,2,3,5]
function foo(x) {
x.push(5);
console.log(x); // x = [1,2,3,5]
x.length = 0;
x.push(5,6,7,8);
console.log(x); // x = [5,6,7,8]
}
var a = [1,2,3];
foo(a);
console.log(a); // a = [5,6,7,8]
var a = 2;
var b = Number(a);
var c = new Number(a);
console.log(typeof b); // number
console.log(typeof c); // object
function foo(x) {
x += 1;
}
foo(b);
foo(c);
console.log(b); // b = 2
console.log(c); // c = 2
참고자료(출처): EXLSkills 공식 홈페이지, [MDN Web Docs](https://developer.mozilla.org/ko/docs/Glossary/Hoisting)
You don't know JS(도서)