JavaScript Syntax is a set of rules that defines the structure of JavaScript program.
Table of Contents
Javascript Syntax Explained
Let's discuss JavaScript Syntax in detail. JavaScript Syntax include many things that we use while writing JavaScript code.
JavaScript syntax is pretty simple, it defines the set of rules about the structure of JavaScript code. A simple JavaScript instruction might contain a keyword, a variable, a value, an operator and a semicolon.
Semicolon is optional if statements are on different lines but it's a must when more than one statements are on single line, but its always a good practice to use semicolon after each statement.
JavaScript Syntax Error
JavaScript Syntax Error occurs when JavaScript Engine notice any violation of any rule that defines for JavaScript Syntax. In JavaScript the Syntax Error occurs when JavaScript Engine is parsing the code or during the interpretation of the JavaScript Code.
Understanding JavaScript Syntax
Highlights of JavaScript Syntax are JavaScript Statements, JavaScript Expressions, Case Sensitivity in JavaScript, Camel Case in JavaScript, JavaScript Identifiers, JavaScript Keywords, JavaScript Operators, JavaScript Comments, JavaScript Variables, JavaScript Values, JavaScript Literals and JavaScript Character Set.
JavaScript Statements
JavaScript Statement is a combination of keywords, values, operators, expression, comments, identifiers etc. All three lines of code written below are example of JavaScript Statements.
var a, b, c;
a = 1;
b = 2;
c = a + b;
JavaScript Keyword
JavaScript Keyword are predefined keywords that are used to perform a specific task. In the above example, var is a JavaScript Keyword.
Var is used to create new variables.
var name;
JavaScript Operators
JavaScript Operators are used to perform different operations on variables or values. JavaScript uses arithmetic operators ( + - * / ) to calculate values.
var a = 1;
var b = a + 2;
JavaScript Values
JavaScript has two types of values. Fixed values and Variable values. Fixed values are called literals and Variable values are called variables.
JavaScript Variables
JavaScript Variables are used to store some kind of value in it, like numbers or string etc. Var is used to create a variable in JavaScript and value is assigned to that variable using equal sign.
var name = "How To Code School";
JavaScript Literals
Fixed values are called literals in JavaScript. Two main types of literals are numbers and strings.
var string = "How To Code School";
var number = 1;
JavaScript Identifiers
JavaScript Identifiers is the name of the variable or used to name variables. Identifiers identify variables. Read JavaScript naming rules here.
JavaScript Expression
JavaScript Expression is a combination of Variable, Values and Operators which gives another value as a result. This computation is called evaluation.
var number = 1 + 10;
JavaScript Comments
JavaScript comments are made using double forward slashes or a forward slash with an asterisk. Any statement or line after double slash (//) or between /* and */ will not be executed and will be treated as a comment.
// This is a JavaScript comment.
/*
This is also JavaScript comment.
*/
JavaScript Case Sensitivity
JavaScript is a case sensitive language. Meaning that myValue and myvalue are two different identifiers or variables. So while writing Keywords, Function names, variables or any other identifier we should be careful.
Camel Case in JavaScript
There are many ways to name variables, functions etc. Coders use underscore (_) upper and lower case letters and their combination.
One method is camel case naming method. There are two types of camel case methods in JavaScript. Upper Camel Case and Lower Camel Case.
In Upper Camel Case method, first letter of every word is capital. Example: FirstName, LastName etc.
In Lower Camel Case method, first letter of every word would be in upper case except for one word, the first one. Example: firstName, lastName etc.
White Spaces in JavaScript
JavaScript ignores all the white spaces, you can write your JavaScript code anywhere you want, after as many lines or spaces you want. It doesn't matter but as mentioned earlier if two statements are on same line then they should be separated using a semicolon.