티스토리 뷰

 

 

 [] for Array

 {} for Object 

 

 

document.write

- It's a DOM method that allows writing HTML expressions and JavaScript code to a document.  

- DOM에서 html처럼 text를 쓸 수 있는 method.

 

 

 

Example code

<script>
    var food = {
        "fruit" : "orange", //객체의 property는 key 와 value로 이루어져있다
        "veggie":"onion",
    };
    //print the object
    document.write("fruit : " + food.fruit + "</br>");
    document.write("veggie : " + food.veggie + "</br>");
 
    //add more information
    food.snack = "chips";
    document.write("snack : " + food.snack + "</br>");
 
    //blank can be added using [" "]
    food["cold drink"= "orange juice"
    document.write("drink : " + food["cold drink"+ "</br>");
</script>
 

*key에 공백(띄어쓰기)을 넣어싶으면  [] 를 사용해서 넣어준다. e.g. ["orange  juice"]

 

 

 

Result

fruit : orange
veggie : onion
snack : chips
drink : orange juice

 

 

 

 

Reference

https://opentutorials.org/course/3085/18853

'JavaScript' 카테고리의 다른 글

Callback function in JavaScript  (0) 2019.08.28
Arrow function in JavaScript  (0) 2019.08.28
Closure in JavaScript  (0) 2019.08.27
'This' in JavaScript  (0) 2019.08.27
String and Number  (0) 2019.08.05