JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. This includes statements, expressions, variables, functions, operators, and control flow constructs.
The syntax is the set of rules that determines how JavaScript programs are constructed.
// Variable declaration let c, d, e;// Assign value to the variable c = 5;
// Compute value of variables d = c; e = c / d;
console.log("Basic Print method in JavaScript");
There are two main types of values defined in JavaScript syntax:
Syntax rules for JavaScript fixed values (literals) include:
let num1 = 50; let num2 = 50.05; let str1 = "Intricate"; let str2 = 'Devo'; console.log(num1); // Output: 50 console.log(num2); // Output: 50.05 console.log(str1); // Output: Intricate console.log(str2); // Output: Devo
A JavaScript variable is a simple name for a storage location where data is kept.
// Global variable declaration let fruitName = "Apple";function showVariables() { // Local variable declaration let num = 45; // Display the value of the Global variable console.log(fruitName); // Output: Apple // Display the value of the Local variable console.log(num); // Output: 45 }
// Function call showVariables();
JavaScript operators are symbols used to compute values or perform operations on operands.
+, -, *, /) are used to compute numeric values.=, +=, %=) are used to assign values to variables.// Variable Declarations let x, y, sum;// Assign values to the variables x = 3; y = 23;
// Use an arithmetic operator to add two numbers sum = x + y;
console.log(sum); // Output: 26
A JavaScript expression is a combination of values, operators, and variables used to compute a new value.
// Variable Declarations let x, y, num, sum;// Assign values x = 20; y = 30;
// Expression to divide a number num = x / 2;
// Expression to add two numbers sum = x + y;
console.log(num); // Output: 10 console.log(sum); // Output: 50
Keywords are reserved words that have special meanings in JavaScript and tell the browser to perform specific actions.
// 'let' is a keyword used to define a variable let a, b;// 'function' is a keyword which tells the browser to create a function function myAction() {}
Comments are ignored by the JavaScript compiler. They increase code readability by adding suggestions, information, and warnings.
//./* and */.JavaScript provides different data types to hold varying values. JavaScript is a dynamically typed language, meaning you do not need to specify the exact type of a variable upfront.
// String let txt = "IntricateDevo";// Number let a = 5;
// Boolean (Strict equality returns true) console.log(a === 5);
// Array let places = ["ID", "Computer", "Hello"];
// Object let student = { firstName: "Johnny", lastName: "Diaz", age: 35, mark: "blueEYE" };
JavaScript functions are blocks of reusable code used to perform particular operations. A function executes only when something calls (invokes) it.
// Function definition
function displayResult() {
let num = 45;
console.log(num);
}
// Function call
displayResult(); // Output: 45
Identifiers are names used to label variables, keywords, and functions.
Naming Rules: An identifier must begin with:
A-Z or a-z)$)_)Note: Numbers are not allowed as the first character in JavaScript identifiers.
Case Sensitivity:
JavaScript identifiers are highly case-sensitive. The variables firstName and firstname are considered two entirely different variables.
Camel Case:
In JavaScript, Camel Case is the preferred naming convention (e.g., firstName, lastName).
Character Set: JavaScript uses the Unicode character set, which covers almost all characters, punctuations, and symbols worldwide.
Which of the following characters is NOT allowed as the first character of a JavaScript identifier?