JavaScript Syntax | Statements and Comments - Variables and Data Types - Operators and Control Flow Statements - Functions in Syntax of JavaScript

JavaScript Syntax:

JavaScript is a versatile scripting language that is primarily used for client-side web development. Here are some key elements of JavaScript syntax:

1. Statements and Comments:

Statements: JavaScript code is composed of statements, which are instructions that perform actions. Statements are typically terminated by a semicolon (`;`).

javascript
  var x = 5;  // Variable declaration and assignment statement
  console.log(x);  // Function call statement


Comments: Comments are used to add explanatory notes to the code. Single-line comments start with `//`, and multi-line comments are enclosed between `/*` and `*/`.

javascript
  // This is a single-line comment
  
  /*
  This is a
  multi-line comment
  */

Statements and Comments - Variables and Data Types - Operators and Control Flow Statements - Functions in Syntax of JavaScript
JavaScript Syntax

2. Variables and Data Types:

Variables:Variables are used to store and represent data. In JavaScript, you can use the `var`, `let`, or `const` keyword to declare variables.

javascript
  var name = "John";  // Variable declaration with initial value
  let age = 25;        // Variable declaration with let
  const pi = 3.14;     // Variable declaration with const


Data Types: JavaScript has dynamic typing, meaning the data type of a variable is determined at runtime. Common data types include numbers, strings, booleans, objects, arrays, and functions.

javascript
  var num = 42;          // Number
  var text = "Hello";    // String
  var flag = true;       // Boolean
  var person = {         // Object
      firstName: "John",
      lastName: "Doe"
  };
  var numbers = [1, 2, 3]; // Array

3. Operators:

JavaScript supports various operators for performing operations on variables and values.

Arithmetic Operators:

javascript
  var sum = 5 + 3;
  var difference = 8 - 3;
  var product = 4 * 2;
  var quotient = 10 / 2;

Comparison Operators:

javascript
  var isEqual = (a === b);
  var isNotEqual = (a !== b);
  var greaterThan = (x > y);
  var lessThanEqual = (x <= y);

Logical Operators:

javascript
  var andCondition = (a && b);
  var orCondition = (x || y);
  var notOperator = !isTrue;

4. Control Flow Statements:

Conditional Statements:

javascript
  if (condition) {
      // Code to execute if the condition is true
  } else {
      // Code to execute if the condition is false
  }

Loops:

javascript
  for (var i = 0; i < 5; i++) {
      // Code to repeat in each iteration
  }

  while (condition) {
      // Code to repeat as long as the condition is true
  }

Switch Statement:

javascript
  switch (expression) {
      case value1:
          // Code to execute if expression equals value1
          break;
      case value2:
          // Code to execute if expression equals value2
          break;
      default:
          // Code to execute if expression doesn't match any case
  }

5. Functions:

Functions are blocks of reusable code that can be defined and called.

javascript
function greet(name) {
    return "Hello, " + name + "!";
}

var greeting = greet("John");
console.log(greeting);

This is a basic overview of JavaScript syntax. JavaScript is a versatile language with many features, and its syntax allows developers to create dynamic and interactive web applications.
ShowHideComments