본문 바로가기

Javascript

Javascript

<script> : 자바스크립트는 스크립트 태그에 한데 모아서 작성할 수 있다. <head>와 <body> 두 곳 다 위치할 수 있다

 

getElementById() : id로 html 요소를 찾아 접근하는데에 쓰인다

 

id : html 요소 정의

 

innerHTML : html 요소안의 컨텐츠 정의

 

document.write() : 쉽게 html에 결과작성할 수 있지만 html 페이지 로드 후 사용된다면 현재 존재하는 html 페이지의 내용을 삭제해버리므로 test용도로 적절한 function

 

window.alert() : 알림창 띄우기. window 키워드는 생략가능

 

console.log() : 브라우저가 데이타를 보여준다 (f12 -> Consol -> 새로고침하면 입력한 값을 볼 수 있다)

 

window.print() : 현재 페이지를 인쇄할 수 있다


Variables

자바스크립트에서는 변수를 다시 정의해도 기존 값을 유지할 수 있다

String에 숫자를 더하면 계산된ㄴ것이 아니라 그냥 이어진다 ex) "5"+2+3 = 523

자바스크립트에서의 변수명은 A-Z, a-z, $, _, 들을 쓸 수 있다

할당이 안된 변수는 undefined로 표기된다. string은 예외

 

Array Object

자바스크립트의 배열은  [ " ", " ", " ", ...] 의 형식으로 한다

자바스크립트 object는 { name:value pair, name2:value pair2, ... } 로 쓰인다

 

undefined와 null

typeof undefined           // undefined
typeof null                // object

null === undefined         // false
null == undefined          // true

 

Javascript Events

onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page


setTimeout(function(){alert('test');},3000); 
setInterval(function(){alert('test');},3000); 
clearInterval


String

indexOf() : returns the index of the first occurrence of a specified text in a string

lastIndexOf() : returns the index of the last occurrence of a specified text in a string

 

slice(start, end)
substring(start, end)
substr(start, length)

replace() : /i 는 대소문자 구별없이 바꾸며 /g는 모든 문자가 맞아야한다

str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3Schools");

str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3Schools");

 

toUpperCase()

toLowerCase()

concat() : joins two or more strings

trim()

charAt(position)
charCodeAt(position)
Property access [ ]

  • It makes strings look like arrays (but they are not)
  • If no character is found, [ ] returns undefined, while charAt() returns an empty string.
  • It is read only. str[0] = "A" gives no error (but does not work!)

charCodeAt()

split()


Number

NaN is a JavaScript reserved word indicating that a number is not a legal number.

ex) var x = 100 / "apple" 

 

Infinity (or -Infinity) 줄 수 있는 가장 큰 수를 준다

 - 0으로 나눴을때도 infinity 값을 준다

 - infinity의 type은 nuimber이다.


Array

Array를 다룰 때 어떤 배열 변수를 typeof 함수로 타입을 확인하면 object로 나온다

solution1: isArray 함수를 사용

solution2: 직접 생성한 isArray 함수를 사용

solution3: instanceof 를 사용 ex) 변수 instanceof Array;

 

toString() / join() : 배열을 string으로 만들때

pop() push() : 배열 끝에서 빼거나 추가. push() returns the new array length.

shift() unshift() : 배열 앞에서 빼거나 추가. unshift() returns the new array length.

delete : 배열에서 삭제

 

map() : 메소드를 적용해 새로운 배열을 만든다

filter() :  조건에 부합하는 것으로 새로운 배열생성


Math

abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
acosh(x) Returns the hyperbolic arccosine of x
asin(x) Returns the arcsine of x, in radians
asinh(x) Returns the hyperbolic arcsine of x
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
atanh(x) Returns the hyperbolic arctangent of x
cbrt(x) Returns the cubic root of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, ..., n) Returns the number with the highest value
min(x, y, z, ..., n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) Returns the sine of x (x is in radians)
sinh(x) Returns the hyperbolic sine of x
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
tanh(x) Returns the hyperbolic tangent of a number
trunc(x) Returns the integer part of a number (x)

 


Regular Expression Modifiers

i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching

let 키워드는 같은 이름의 변수를 생성하는데에서 생기는 혼란을 막아준다

'Javascript' 카테고리의 다른 글

Javascript AJAX  (0) 2020.07.29
Javascript Browser BOM  (0) 2020.07.29
Javascript Form & HTML DOM  (0) 2020.07.28