http://www.w3schools.com/js/js_statements.asp
JavaScript Statements
In HTML, JavaScript statements are "instructions" to be "executed" by the web browser.
JavaScript Statements
HTML요소중에 id가 demo인것 안에 Hello Dolly. 라고 쓴다.
Example
document.getElementById("demo").innerHTML = "Hello Dolly.";
JavaScript Programs
Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are written.
In this example, x, y, and z is given values, and finally z is displayed:
Example
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
JavaScript programs (and JavaScript statements) are often called JavaScript code. |
Semicolons ;
세미콜론으로 자바스크립트문을 구분한다.
a = 5;
b = 6;
c = a + b;
세미콜론으로 구분하면 한줄에 여러개 써도 된다.
a = 5; b = 6; c = a + b;
On the web, you might see examples without semicolons. Ending statements with semicolon is not required, but highly recommended. |
JavaScript White Space
자바스크립트는 공백을 무시한다. 가독성을 위해 공백을 추가해도 좋다.
밑에 두줄은 같은 소스다.
var person = "Hege";
var person="Hege";
A good practice is to put spaces around operators ( = + - * / ):
var x = y + z;
JavaScript Line Length and Line Breaks -가독성을 위해
For best readability, programmers often like to avoid code lines longer than 80 characters.
If a JavaScript statement does not fit on one line, the best place to break it, is after an operator:
Example
document.getElementById("demo").innerHTML =
"Hello Dolly.";
JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, are in JavaScript functions:
Example
function myFunction() {
document.getElementById("demo").innerHTML = "Hello Dolly.";
document.getElementById("myDIV").innerHTML = "How are you?";
}
In this tutorial we use 4 spaces of indentation for code blocks. You will learn more about functions later in this tutorial. |
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
Here is a list of some of the keywords you will learn about in this tutorial:
Keyword | Description |
---|---|
break | Terminates a switch or a loop |
continue | Jumps out of a loop and starts at the top |
debugger | Stops the execution of JavaScript, and calls (if available) the debugging function |
do ... while | Executes a block of statements, and repeats the block, while a condition is true |
for | Marks a block of statements to be executed, as long as a condition is true |
function | Declares a function |
if ... else | Marks a block of statements to be executed, depending on a condition |
return | Exits a function |
switch | Marks a block of statements to be executed, depending on different cases |
try ... catch | Implements error handling to a block of statements |
var | Declares a variable |
자바스크립트 키워드는 예약되어 있다. 예약어는 변수 이름으로 사용할 수 없다. |
'차근차근 > JAVA Script' 카테고리의 다른 글
JavaScript Variables (0) | 2016.02.15 |
---|---|
JavaScript Comments _ 주석처리 (0) | 2016.02.15 |
JavaScript Output (0) | 2016.02.15 |
JavaScript Where To (0) | 2016.02.15 |
HTML 렌더링 순서에 따른 <script> 태그 사용 권장사항 (0) | 2016.02.01 |