Basics
Code Conventions
Info:
Add a semicolon at the end of each executable statement
wrap line after an operators
document.getElementById("demo").innerHTML =
"Hello Dolly!";
Danger:
avoid code lines longer than 80 characters
Hyphens are not allowed in JavaScript. They are reserved for subtractions.
Syntax
Info:
- values
- Fixed values are called Literals
- Variable values are called Variables
Danger:
- An equal sign
=
is used to assign values to variables. - Numbers are not allowed as the first character in names.
Placement
Info:
- You can place any number of scripts in an HTML document.
- can be placed in the
<body>
, or in the<head>
section- Placing at bottom
<body>
element improves the display speed, because script interpretation slows down the display.
- Placing at bottom
inline
<!DOCTYPE html>
<html>
<body>
<script>
function foo() {
console.log("Hello World!");
}
</script>
</body>
</html>outsourced / external file
<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>full url
<script src="https://www.foo/js/myScript.js"></script>
Comments
Info:
It is most common to use single line comments.
Block comments are often used for formal documentation.
line comment
let x, y, z; // Statement 1
block comment
/*
block
comment
*/