DONATE  
MENU
AJAX
JAVASCRIPT
JSON
HTML

JavaScript Tutorials

INTRODUCTION

» 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

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» 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

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» 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.
<script>
alert("Welcome!");
</script>
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
<script>
alert(1);
</script>
myscript.js - CORRECT WAY
alert(1);
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
alert(num)
RIGHT
var num = 56;
alert(num);
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.
SUMMARY
  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.
cent?

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

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





StackBeginner 2021 © Joseph Omamode