JavaScript Introduction | Overview | JavaScript Syntax
What Is a Program?
A program consists of instructions that a computer can execute. In computer programming, instructions that a computer can execute are called statements. In other words, a program can be described as a set of statements executed by a computer to obtain a specific result.
JavaScript Syntax
JavaScript statements are separated by semicolons (;).
var x = 10;
var result = x + 3
JavaScript is case-sensitive. When writing or using variable names, function names, reserved words, and similar identifiers in JavaScript, you must distinguish uppercase and lowercase letters accurately.
var javascript = 10; // The variables javascript and JavaScript are recognized as two different variables.
var JavaScript = 20;
Var Script = 30; // Variables can be declared only with the var keyword, so Var does not work.
Literal
A literal means the directly expressed value itself.
All values in the following example are literals.
12 // Numeric literal
"JavaScript" // String literal
'Hello' // String literal
true // Boolean literal
Identifier
An identifier means a name used when writing the name of a variable or function. In JavaScript, identifiers can use only letters, numbers, underscores (_), or dollar signs ($).
In JavaScript, identifiers cannot start with a number so that numbers and identifiers can be quickly distinguished. JavaScript uses the Unicode character set.
Identifier Naming Styles
In JavaScript, the following naming styles can be used when writing identifiers.
- Camel Case
- Underscore Case
Camel Case is a style where, when an identifier consists of several words, the first word is written entirely in lowercase and each following word starts with an uppercase letter. Underscore Case is a style where the words that make up an identifier are written only in lowercase and connected with underscores (_).
In JavaScript, Camel Case is commonly used by convention when writing identifiers. Therefore, in these lessons, only Camel Case will be used for code readability and consistency.
var firstVar = 10; // Camel Case
function my_first_func { // Underscore Case
var firstLocalVar = 20; // Camel Case
}
In JavaScript, the hyphen (-) is a reserved keyword for subtraction, so it cannot be used when writing identifiers.
Keyword
In JavaScript, some words are reserved in advance for special purposes. These pre-reserved words are called keywords or reserved words. These keywords cannot be used as identifiers in a program.
var firstVar = 10; // var is a keyword reserved for defining variables.
function myFirstFunc { // function is a keyword reserved for defining functions.
var secondVar = 20; // var is a keyword reserved for defining variables.
}
Comment
A comment is a kind of explanatory text inserted in code. Comments can be used as references when the author or another developer modifies the code later, and they are also used for debugging during web page development. Comments can be written anywhere in JavaScript code and have no effect on the behavior of the web browser.
JavaScript supports the following two comment formats.
Syntax
1. Single-line comment: // comment
2. Multi-line comment: /* comment */
var x = 10;
// var y = 20; Single-line comment: this part will not be executed.
/*
x = x + y;
Multi-line comment:
This part also will not be executed.
*/
document.getElementById("text").innerHTML = x;
The following example inserts another comment inside a multi-line comment in JavaScript.
/* Multi-line
// You can insert another single-line comment inside a two-line comment like this.
comment. */
As in the example above, JavaScript allows another single-line comment inside a multi-line comment.
However, as in the following example, you cannot insert another multi-line comment inside a multi-line comment.
/* Multi-line
/* Another multi-line comment. */
comment. */
If another multi-line comment is inserted inside a multi-line comment as in the example above, the symbol (*/) that marks the end of the inserted comment is recognized as the symbol (*/) that ends the first comment. Therefore, in the example above, the third line is not properly recognized as a comment, an Uncaught SyntaxError occurs, and the running script stops.
Therefore, multi-line comments in JavaScript must never be nested.