DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Max Lockwood
Max Lockwood

Posted on • Originally published at maxlockwood.dev

What is the Syntax of JavaScript?

What is the Syntax of JavaScript?

Javascript is a programming language used by programmers all over the world to develop dynamic and interactive web content such as applications and browsers. JavaScript is the most widely used programming language in the world, with 97.0% of all websites using it as a client-side programming language.

JavaScript syntax is the collection of rules that control how JavaScript programs are built.

Basic Syntax

<script>
 document.write("Basic Print method in JavaScript");
</script>
Enter fullscreen mode Exit fullscreen mode

JavaScript Variables

Variables in JavaScript: A JavaScript variable is simply the name of the storage location where data will be kept.

Variables in JavaScript are divided into two types which are listed below:

  • Local variables: Are variables that are declared within a block or function.
  • Global variables: Global variables are declared outside of a function.

Example:

// How to create variables:
var x;
let y;
const z;

// How to use variables:
x = 5;
y = 6;
let z = x + y;
Enter fullscreen mode Exit fullscreen mode

JavaScript Values

The JavaScript syntax defines two types of values: Fixed values and variable values.
Fixed values are called literals. Variable values are called variables.

JavaScript Literals

Number literals can be written with or without decimal places. Number literals can be either positive numbers or negative numbers. If you do not specify a sign, then a positive number is assumed.

8.50
2001
-24
Enter fullscreen mode Exit fullscreen mode

String literals are text and are always surrounded by single quotes (') or double quotes (").

'Joe Bloggs'
"Joe Bloggs"
Enter fullscreen mode Exit fullscreen mode

JavaScript Variables

Variables are used in programming languages to β€œstore” data values.
Variables are declared in JavaScript using the keywords var, let, and const.
To β€œassign values” to variables, use the equal sign (=).

// Declaring a variable
let x;
// Assigning a variable
x = 8;
Enter fullscreen mode Exit fullscreen mode

JavaScript Operators

In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables).

JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.

Here is a list of the different types of operators

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Conditional Operators
  • Type Operators

Example of an arithmetic operator (+):

2 + 3;
// 5
Enter fullscreen mode Exit fullscreen mode

JavaScript uses an assignment operator (=) to assign values to variables:

let x, y;
x = 5;
y = 6;
Enter fullscreen mode Exit fullscreen mode

JavaScript Expressions

A set of values, variables, and operators combined to produce a value is called an expression.
An evaluation is the name of the calculation.

Example:

// For example, 2 * 10 evaluates to 20:
2 * 10
Enter fullscreen mode Exit fullscreen mode

Expressions can also contain variable values:

x * 10
Enter fullscreen mode Exit fullscreen mode

JavaScript Keywords

JavaScript keywords are used to identify actions to be performed.
The let keyword tells the browser to create variables:

let x, y;
x = 6 + 6;
y = x * 10;
//Evaluates to 120
Enter fullscreen mode Exit fullscreen mode

JavaScript Comments

Not every JavaScript statement is β€œexecuted.”
Code that follows a double slash // or that is between a /* and /* is regarded as a comment.
Comments are disregarded and won’t be executed:

let n = 5;   // I will be executed
// n = 10;   I will NOT be executed
Enter fullscreen mode Exit fullscreen mode

JavaScript Identifiers

In JavaScript, identifiers are names that represent a variable, function, or property.
JavaScript names are case-sensitive and have to start with:

  • A letter (A-Z or a-z)
  • An underscore (_)
  • or the dollar sign ($)

Characters beyond that could be letters, numbers, underscores, or dollar signs.

JavaScript is Case Sensitive

JavaScript is a case-sensitive programming language. This means that all language keywords, variables, function names, and other identifiers must be entered with consistent letter capitalisation.

For example, myVariable is not the same as myvariable. If you have problems in your code, check the case!

Example, the variables firstName and firstname, are two different variables:

let firstName, firstname;
firstName = "Joe";
firstname = "Andy";
Enter fullscreen mode Exit fullscreen mode

JavaScript and camelCase

Real life JavaScript programming is very visible and should follow specific naming conventions and rules.

CamelCase

  • A common naming convention that employs the practice of writing phrases with each word or abbreviation in the centre beginning with a capital letter e.g. myVariable
  • Helps to join multiple words together without using underscores
  • Avoids the use of the hyphen (-) character which isn’t allowed in many languages
  • Provides the ability to create very descriptive variable names:
  • myVariable()
  • myFunction()
  • addNumbers()
  • modifyBodyElement()

Conclusion

This article offered an overview of JavaScript’s basic syntax and code structure. Syntax is vital for both execution of a program and readability and maintainability for both yourself and collaborators on your code.

See also

What exactly is JavaScript?
What are the Three Variables in JavaScript?
How to Display Output in JavaScript

Thanks for reading!

If you liked this article, then please share. You can also find me on Twitter for more updates.

Top comments (0)

CLI tools you won't be able to live without πŸ”§

CLI tools