Solve common problems in your JavaScript code
๋ค์์ ๋งํฌ๋ค์ ๋น์ ์ JavaScript ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ์๋ํ ์ ์๊ฒ ๊ณ ์ณ์ผ๋๋ ํํ ๋ฌธ์ ๋ค์ ํด๊ฒฐ์ฑ ์ ์ ์ํ๋ค.
์ด๋ณด์๋ค์ ํํ ์ค์๋ค
>์คํ ๋ง๊ณผ ๋์๋ฌธ์๋ฅผ ์ ๋๋ก ํด๋ผ
์ฝ๋๊ฐ ์๋ํ์ง ์๊ฑฐ๋ ๋ธ๋ผ์ฐ์ ๊ฐ ๋ฌด์ธ๊ฐ๊ฐ undefined๋ผ๊ณ ๋ถํํ๋ฉด ๋ชจ๋ ๋ณ์ ์ด๋ฆ, ํจ์ ์ด๋ฆ ๋ฑ์ ์ ํํ๊ฒ ์ ๋ ฅํ๋์ง ํ์ธํ์ญ์์ค.
๋ฌธ์ ๋ฅผ ์ผ์ผํค๋ ๋ช ๊ฐ์ง ์ผ๋ฐ์ ์ธ ๋ด์ฅ ๋ธ๋ผ์ฐ์ ํจ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
| ์ฌ๋ฐ๋ฅธ | ์๋ชป๋ |
|---|---|
getElementsByTagName() |
getElementbyTagName() |
getElementsByName() |
getElementByName() |
getElementsByClassName() |
getElementByClassName() |
getElementById() |
getElementsById() |
์ธ๋ฏธ์ฝ๋ก ์ ์์น
์ธ๋ฏธ์ฝ๋ก ์ ์์น๊ฐ ์๋ชป ๋์ง ์์๋์ง ํ์ธํ์ธ์. ์๋ฅผ๋ค์ด:
| ์ฌ๋ฐ๋ฅธ | ์๋ชป๋ |
|---|---|
elem.style.color = 'red'; |
elem.style.color = 'red;' |
ํจ์
ํจ์์ ๊ด๋ จํ์ฌ ์ค์ํ ์ ์๋ ๊ฒ๋ค์ด ๋ง์ด ์์ต๋๋ค.
๊ฐ์ฅ ํํ ์ค์๋ ํจ์๋ฅผ ์ ์ธํ๊ณ ์ด๋์๋ ํธ์ถํ์ง ์๋ ๊ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด:
function myFunction() {
alert("This is my function.");
}
์ ์ฝ๋๋ ํธ์ถํ์ง ์๋ ์ด์ ์๋ฌด๊ฒ๋ ํ์ง ์์ต๋๋ค.
myFunction();
ํจ์ ์ค์ฝํ
ํจ์๋ ๊ฐ์์ ์ค์ฝํ๊ฐ ์์์ ๋ช ์ฌํ์ธ์ โ ๋ณ์๋ฅผ ์ ์ญ์ผ๋ก ์ ์ธํ๊ฑฐ๋ ํจ์์์ ๊ฐ์ ๋ฆฌํดํ์ง ์๋ํ ํจ์ ๋ฐ์์ ํจ์ ๋ด๋ถ์ ์ค์ ๋ ๋ณ์ ๊ฐ์ ์ ๊ทผ ํ ์ ์์ต๋๋ค. (์ฆ, ์ด๋ ํ ํจ์ ๋ด์์๋ ์ ๊ทผ ์๋จ)
๋ฆฌํด ๋ฌธ ์ดํ ์ฝ๋ ์คํ
ํจ์ ๋ฐ์ผ๋ก ๊ฐ์ ๋ฆฌํดํ ๋, JavaScript ์ธํฐํ๋ฆฌํฐ๊ฐ ํจ์๋ฅผ ์์ ํ ๋น ์ ธ๋๊ฐ์ ๋ช ์ฌํ์ธ์. โ ๋ฆฌํด ๋ฌธ ์ดํ์ ์ ์ธ๋ ์ฝ๋๋ ์ ๋๋ก ์คํ๋์ง ์์ต๋๋ค.
์ฌ์ค, ๋ช๋ช ๋ธ๋ผ์ฐ์ ๋ค์ (Firefox ์ฒ๋ผ) ๋ฆฌํด ๋ฌธ ์ดํ์ ์ฝ๋๊ฐ ์๋ค๋ฉด ๊ฐ๋ฐ์ ์ฝ์์ ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ์ค๋๋ค. Firefox๋ "unreachable code after return statement" ๋ผ๊ณ ์๋ ค์ค๋๋ค.
์ค๋ธ์ ํธ ํ๊ธฐ vs ์ผ๋ฐ์ ํ ๋น
๋ณดํต ์๋ฐ์คํฌ๋ฆฝํ ์ด์ ๋ฌด์ธ๊ฐ๋ฅผ ํ ๋นํ๋ ค๊ณ ํ ๋, ๋จ์ผ ๋ฑํธ ๊ธฐํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์:
const myNumber = 0;
ํ์ง๋ง, ์ด ๋ฐฉ๋ฒ์ ์ค๋ธ์ ํธ์์ ๋์ํ์ง ์์ต๋๋ค. โ ์ค๋ธ์ ํธ์์ ๋ฉค๋ฒ์ ๊ฐ ์ฌ์ด๋ฅผ ์ฝ๋ก ์ผ๋ก ๊ตฌ๋ถํ๊ณ , ๊ฐ ๋ฉค๋ฒ๋ค์ ์ฝค๋ง๋ก ๊ตฌ๋ถํฉ๋๋ค, ์๋ฅผ ๋ค์ด:
const myObject = {
name: "Chris",
age: 38,
};
๊ธฐ๋ณธ ์ ์
Basic use cases
>General
Variables
Math
- What types of number do you have to deal with in web development?
- How do you do basic math in JavaScript?
- What is operator precedence, and how is it handled in JavaScript?
- How do you increment and decrement values in JavaScript?
- How do you compare values in JavaScript? (e.g. to see which one is bigger, or to see if one value is equal to another).
Strings
- How do you create a string in JavaScript?
- Do you have to use single quotes or double quotes?
- How do you escape characters in strings?
- How do you join strings together?
- Can you join strings and numbers together?
- How do you find the length of a string?
- How you find what character is at a certain position in a string?
- How do you find and extract a specific substring from a string?
- How do you change the case of a string?
- How do you replace one specific substring with another?
Arrays
Debugging JavaScript
- What are the basic types of error?
- What are browser developer tools, and how do you access them?(en-US)
- How do you log a value to the JavaScript console?(en-US)
- How do you use breakpoints, and other JavaScript debugging features?(en-US)
For more information on JavaScript debugging, see Handling common JavaScript problems(en-US); also see Other common errors for a description of common errors.
Making decisions in code
- How do you execute different blocks of code, depending on a variable's value or other condition?
- How do you use if ...else statements?
- How do nest one decision block inside another?
- How do you use AND, OR, and NOT operators in JavaScript?
- How do you conveniently handle a large number of choices for one condition?
- How do you use a ternary operator to make a quick choice between two options based on a true or false test?
Looping/iteration
- How do you run the same bit of code over and over again?
- How do you exit a loop before the end if a certain condition is met?
- How do you skip to the next iteration of a loop if a certain condition is met?
- How do you use while and do ... while loops?
- How to iterate over the elements in an array
- How to iterate over the elements in a multidimensional array
- How to iterate over the members in an object
- How to iterate over the members of an object nested inside an array
Intermediate use cases
>Functions
- How do you find functions in the browser?
- What is the difference between a function and a method?
- How do you create your own functions?
- How do you run (call, or invoke) a function?
- What is an anonymous function?
- How do you specify parameters (or arguments) when invoking a function?
- What is function scope?
- What are return values, and how do you use them?
Objects
- How do you create an object?
- What is dot notation?
- What is bracket notation?
- How do you get and set the methods and properties of an object?
- What is
this, in the context of an object? - What is object-oriented programming?
- What are constructors and instances, and how do you create them?
- What different ways are there to create objects in JavaScript?
JSON
Events
- What are event handlers and how do you use them?
- What are inline event handlers?
- What does the
addEventListener()function do, and how do you use it? - Which mechanism should I use to add event code to my web pages?
- What are event objects, and how do you use them?
- How do you prevent default event behaviour?
- How do events fire on nested elements? (event propagation, also related โ event bubbling and capturing)
- What is event delegation, and how does it work?