JavaScript Tutorials


» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls


» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement


» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop


» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions


» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays


» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword


» Introduction to AJAX


» Introduction to JSON


» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)


» Math Object

» Math Methods


» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events


» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript


» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Syntax
Share Tweet WhatsApp

JavaScript Syntax

Syntaxes are simply the rules in which the programs of a programming language should be written. JavaScript syntaxes are the rules you should know and follow while writing JavaScript programs on either your webpage or in a separate JavaScript file. JS PROGRAMS The first thing to note is that JavaScript programs can be written directly on a webpage or in a separate JavaScript file. If your programs are written on a webpage e.g mywebpage.html or mywebpage.php, they must be contained in the HTML <script> element else the codes won't be executed because they won't be recognized as JS codes.
On the other hand, if your programs are contained in a separate JavaScript file e.g myscript.js, then, no need enclosing them inside the <script> element since the file is a script and not a webpage that understands HTML markups. myscript.js - this script won't work
myscript.js - CORRECT WAY
JS STATEMENT The next thing to note is that JavaScript programs comprises of statements. A statement is an instruction given to a computer to do something. Now, when you compose or write your respective program's statements, ensure you end each with a semicolon. Due to its "not too strict" nature, JavaScript may overlook the mistake of not ending statements with a semicolon but it is not something to do as your program may fail in a strict mode or environment. WRONG
var num = 56
var num = 56;
JS VARIABLES In JavaScript, we store values in variables and with those variables' names, we can make use of those values at anytime in our program. Here are things to note while working with variables,
  • Ensure you declare any variable you assign a value to it with the var keyword
  • Ensure you assign a value to a variable before using it else it becomes undefined.
  • Be careful so that you don't name two variables that are suppose to be different with same name.
  1. A syntax is a rule that guides the composition of the programs of a programming language.
  2. One of JavaScript syntaxes is to write JavaScript programs as content to the <script> element when such programs are contained in a webpage.
  3. But if programs are written in a JavaScript file, no need enclosing them in the <script> tag.
  4. Separate JavaScript files must have a .js extension.
  5. JavaScript programs mostly comprise of statements.
  6. Statements are instructions given to computers or browsers to do something.
  7. When you write a JavaScript statement, end it with a semi - colon(;).
  8. We use variables to store and give names to values.
  9. Before you give a value to a variable, ensure it has been declared using the var keyword.
  10. Before you use a variable, ensure it contains or you have given a value to it.
  11. Be careful not to give variables that should be different same name. The variable simply carries the last value that was given to it while the previous gets lost.
Take a short quiz on JavaScript Syntax.
Ask a question or tell us if JavaScript Syntax is easy or not.

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.

StackBeginner 2021 © Joseph Omamode