JavaScript syntax

JavaScript Syntax

JavaScript is a programming language. Grammar rules define the structure of a language.

JavaScript syntax

JavaScript is a scripting language.

It is a lightweight, yet powerful programming language.

JavaScript Literal

In a programming language, a literal is a constant, as in 3.14.

Number literal can be an integer or a decimal, or scientific notation (e).

String literal can use single or double quotes:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 'John Doe';
</script>
</body>
</html>

Array literal defines an array:

[40, 100, 1 , 5, 25, 10]

Object literal defines an object:

{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue "}

Function literal defines a function:

function myFunction(a, b) { return a * b;}

JavaScript Variables

In programming languages, variables are used to store data values.

JavaScript uses the keyword var to define variables and the equal sign to assign values ​​to variables

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
<p id="demo"></p>
<script>
var length;
length = 1;
document.getElementById("demo").innerHTML = length;
</script>
</body>
</html>

JavaScript operators

JavaScript uses arithmetic operators to Calculated value:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = (5 + 6) * 10;
</script>
</body>
</html>

The JavaScript language has many types of operators:


## Type                                                                                                                            


Assignment, arithmetic and bit operating symbol = + - * / describe in the JS operator

Conditional, comparison and logical operators == != < #In HTML, a JavaScript statement sends a command to the browser.

Statements are separated by semicolons: x = 5 + 6;y = x * 10;

JavaScript Keywords


JavaScript statements usually begin with keywords. The var keyword tells the browser to create a new variable:


var x = 5 + 6;

var y = x * 10;

JavaScript Keywords


Like any other programming language, JavaScript reserves some keywords for its own use.


JavaScript also reserves some keywords that are not used in the current language version, but will be used in future JavaScript extensions.

JavaScript keywords must begin with a letter, an underscore (_), or a dollar sign ($). Following characters can be letters, numbers, underscores or dollar signs (numbers are not allowed to appear as the first character, so that JavaScript can easily distinguish keywords and numbers).

The following are the most important reserved words in JavaScript (in alphabetical order):

abstract else instanceof super

##boolean enum int switch

##break export interface synchronized


byte extends let this


case false long throw


##catch final native throws


##char finally new transient


class float null true


const for package try

##continue function private typeof

##debugger goto protected var


#default if public void


delete implementations return volatile


do import short while


double in static with


JavaScript Note

Not all JavaScript statements are "commands". The content after double slash // will be ignored by the browser:

// I will not execute

JavaScript data type

JavaScript has multiple data types: numbers , string, array, object, etc.:

var length = 16; #var lastName = "Johnson"; // String Assignment via string literal
var cars = ["Saab", "Volvo", "BMW"]; // Array Assignment via array literal
var person = {firstName:"John", lastName:"Doe"}; // Object assignment through object literal


Concept of data type

Programming language , data type is a very important content.

In order to be able to manipulate variables, it is very important to understand the concept of data types.

If the data type is not used, the following example will not be executed:

16 + "Volvo"

16 How is adding "Volvo" calculated? The above will generate a Is there an error or the following result is output?

"16Volvo"

You can try executing the above code in your browser to see the effect.

In the following chapters you will learn more about data types.

JavaScript function

JavaScript statements can be written within functions, and functions can be referenced repeatedly:

Referencing a function = calling a function (executing statements within the function).

function myFunction(a, b) {

return a * b; // Return the result of multiplying a by b

}

## JavaScript is case-sensitive.

JavaScript is case-sensitive.

When writing JavaScript statements, please pay attention to whether the case switching key is turned off.

The functions getElementById and getElementbyID are different.

Similarly, variables myVariable and MyVariable are also different.

JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers all characters, including punctuation and other characters.

To learn more, study our complete Unicode reference manual.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo"></p> <script> var length; length = 6; document.getElementById("demo").innerHTML = length; </script> </body> </html>
submitReset Code