Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript syntax


May 06, 2021 JavaScript


Table of contents


JavaScript syntax


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


JavaScript syntax

JavaScript is a scripting language.

It is a lightweight but powerful programming language.


JavaScript literally

In a programming language, a word is a constant, such as 3.14.

The literal quantity of number can be an integer or a small number, or a scientific count (e).

3.14

1001

123e5

Try it out . . .

String literally can be used in single or double quotes:

"John Doe"

'John Doe'

Try it out . . .

The literal amount of the expression is used to evaluate:

5 + 6

5 * 10

Try it out . . .

Array literally defines an array:

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

Object literally defines an object:

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

Function literally defines a function:

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


JavaScript variable

In a programming language, variables are used to store data values.

JavaScript uses the keyword var to define variables, and uses equal numbers to assign values to variables:

var x, length

x = 5

length = 6

Try it out . . .

Variables can be accessed by variable names. I n instructional languages, variables are usually variable. The literal quantity is a constant value.

JavaScript syntax The variable is a name. The literal quantity is a value.

JavaScript operator

JavaScript uses arithmetic operators to calculate values:

(5 + 6) * 10

Try it out . . .

JavaScript uses the assignment operator to assign values to variables:

x = 5
y = 6
z = (x + y) * 10

Try it out . . .

The JavaScript language has several types of operators:

Type Instance Describe
Assignments, arithmetic, and bit operators =  +  -  *  / Described in the JS operator
Conditions, comparisons, and logical operators ==  != <  > Described in the JS comparison operator


JavaScript statement

In HTML, javaScript statements issue commands to the browser. The browser knows what to do with the JavaScript statement.

Statements are separated by a sign:

x = 5 + 6;
y = x * 10;


JavaScript keywords

JavaScript statements usually begin with a keyword. Var keywords tell the browser to create a new variable:

var x = 5 + 6;
var y = x * 10;


JavaScript identifier

Like any other programming language, JavaScript retains some identifiers for its own use.

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

The JavaScript identifier must start with a letter, underscore, or a dollar sign ($).

Subsequent characters can be letters, numbers, underscores, or dollar characters (numbers are not allowed as first characters so javaScript can easily separate identifiers and numbers).

Here are the most important reserved words in JavaScript(alphabetical):

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 implements return volatile
do import short while
double in static with

JavaScript comments

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

I won't execute

JavaScript data type

JavaScript has several data types: numbers, strings, arrays, objects, and so on:

var length = 16; N umber is literally assigned by numbers
var points = x * 10; N umber is literally assigned by expression
var lastName = "Johnson"; S tring is literally assigned by string
var cars = ["Saab", "Volvo", "BMW"]; A rray is literally assigned by arrays
var person = {firstName:"John", lastName:"Doe"}; Object is literally assigned by an object


The concept of data types

Data types are a very important part of programming languages.

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

If you do not use data types, the following instances will not be executed:

16 + "Volvo"

16 Plus "Volvo" how is it calculated? Does this result in an error or output the following results?

"16Volvo"

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

You'll learn more about data types in the next chapters.


JavaScript function

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

Refers to a function , which calls a function (executes a statement within a function).

function myFunction(a, b) {
return a * b; R eturns the result of a multiplied by b
}


JavaScript is case sensitive.

JavaScript is case sensitive.

When writing JavaScript statements, be careful whether to turn off the case switch key.

The function getElementById is different from the getElementbyID.

Similarly, the variable myVariable is different from MyVariable.

Tip: In this site's programming practice, you can practice understanding the case sensitivity of JavaScript variables.


JavaScript character set

JavaScript uses Unicode character sets.

Unicode overrides all characters, including characters such as punctuation.

For further information, learn from our complete Unicode reference manual.


Did you know that?

JavaScript syntax In JavaScript, it is common to name rules for hump methods, such as lastName (not lastname).
JavaScript syntax JavaScript is the scripting language. T he browser executes the script code line by line as it reads the code. For traditional programming, all code is compiled before execution.