λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
TIL/JavaScript

Function / Scope / Arrays

by Edlin 2022. 6. 2.
I organized contents by referencing the following site: https://codecademy.com/

II. Function Declaration

1) Function Declaration

function hello() {
	console.log('Hello!');
}

hello();

1️⃣ Default parameter

function hello(name='Sally') {
	console.log('Hello' + name);
}

2️⃣ Resulting value (return)

  • The default resuting value is undefined

 

2) Function Expressions

const helloMessage = function(name) {
	const message = "Hello " + name;
	return message;
};

helloMessage('Sally'); // Hello Sally

 

3) βž• Arrow Function

  • ES6 introduced arrow function syntax
const helloMessage = (name) => {
	const message = "Hello " + name;
	return message;
}; 
helloMessage('Sally'); // Hello Sally

III. Scope

  • Scope defines where variables can be accessed or referenced

 

1) Global Scope

  • declared outside of blocks
  • accessed from anywhere in the code
  • Not recommended

 

2) Block Scope

  • Accessible within {}
  • Recommended
function insideBlock() {
	const greeting = 'hello';
	console.log(greeting);
}

**console.log(greeting); // ReferenceError**

IV. Arrays

  • can make arrays using an array literal []
  • You can access each element using a numbered position called index
  • You can also update the element by accessing an index
let greetings = ['hi', 'ciao', 'nihao'];
console.log(greetings[0]); // hi
  • Arrays can store other arrays
  • You can change inside values of const array
    • Because each value of const is not const
    • Each value of array is accessed through its address, not value inself
    • but cannot const array itself

 

1) Methods

  • .push(values) : adds values at the end of the array
  • .pop() : removes the last item from the array array
  • .shift() : removes the first item from the arrray and returns the removed item
  • .unshift(value or values(,)) : adds values to the first position of the array
  • .splice(from, count, changed_words) : replaces values from from to the amount of count into changed_words
  • .indexOf(target) : returns the index of target
  • .join(' ') : returns the sum of all the elements in the array, and ' ' is added between each item.

'TIL > JavaScript' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

λΉ„λ™κΈ°μ²˜λ¦¬λž€? 비동기 처리 객체 Promiseλž€?  (0) 2022.07.10
Introduction  (0) 2022.05.30

λŒ“κΈ€