JavaScript Syntax

In this chapter, we will cover the basic syntax of JavaScript. This includes whitespace, statements, literals, variables, operators, expressions, keywords, comments, case sensitivity, and camel case. Understanding these concepts is essential for writing and reading JavaScript code.

Whitespace

Whitespace refers to characters that provide the space between other characters. JavaScript has the following whitespace characters:

  • Carriage return (\r)
  • Space ()
  • New Line (\n)
  • Tab (\t)

Whitespace is generally ignored by JavaScript, but it can be used to format code for better readability.

Example

let name = "Arjun";
let age =25;

console.log(name);
console.log(age);

Statements

A statement in JavaScript is an instruction that the browser executes. Statements can include variable declarations, function calls, loops, and conditionals.

Example

let name = "Arjun";
let age = 25;
console.log(name);
console.log(age);

JavaScript Literals

Literals represent fixed values in JavaScript. There are different types of literals:

  • String Literals: "Hello, World!"
  • Number Literals: 100, 3.14
  • Boolean Literals: true, false
  • Array Literals: [1, 2, 3, 4]
  • Object Literals: { name: "Arjun", age: 25 }

Example

let name = "Arjun"; // String literal
let age = 25;      // Number literal
let isStudent = true; // Boolean literal
let fruits = ["Apple", "Banana", "Mango"]; // Array literal
let person = { name: "Arjun", age: 25 }; // Object literal

JavaScript Variables

Variables are used to store data values. In JavaScript, variables are declared using the let, const, or var keywords.

Example

let name = "Arjun";
const age = 25;
var isStudent = true;

JavaScript Operators

Operators are used to perform operations on variables and values. Common operators include:

  • Arithmetic Operators: +, -, *, /, %
  • Assignment Operators: =, +=, -=, *=, /=
  • Comparison Operators: ==, ===, !=, !==, >, <, >=, <=
  • Logical Operators: &&, ||, !

Example

let x = 10;
let y = 5;

let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y;
let remainder = x % y;

JavaScript Expressions

Expressions are combinations of values, variables, and operators that compute to a value. Expressions can be used to assign values to variables or to perform operations.

Example

let x = 10;
let y = 5;

let sum = x + y; // Expression: x + y, evaluates to 15

JavaScript Keywords

Keywords are reserved words that have special meanings in JavaScript. They cannot be used as identifiers.

Common Keywords

  • let: Declares a block-scoped variable.
  • const: Declares a block-scoped, read-only named constant.
  • if: Marks a block of statements to be executed if a condition is true.
  • else: Marks a block of statements to be executed if the same condition is false.
  • for: Marks a block of statements to be executed in a loop.
  • function: Declares a function.
  • return: Exits a function and returns a value.
  • var: Declares a variable (older syntax).

Example

let name = "Arjun";
const age = 25;
if (age > 18) {
  console.log("Adult");
} else {
  console.log("Not an adult");
}

JavaScript Comments

Comments are used to explain code and are ignored by the browser. There are two types of comments in JavaScript: single-line comments and multi-line comments.

Single-line Comments

// This is a single-line comment
let name = "Arjun"; // This is also a single-line comment

Multi-line Comments

/*
 This is a multi-line comment.
 It can span multiple lines.
 */
let age = 25;

JavaScript is Case Sensitive

JavaScript is case sensitive, meaning that myVariable and myvariable are treated as different variables.

Example

let myVariable = "Hello";
let myvariable = "World";

console.log(myVariable); // Output: Hello
console.log(myvariable); // Output: World

JavaScript and Camel Case

Camel case is a common convention for naming variables in JavaScript. In camel case, each word after the first begins with a capital letter, with no spaces between words.

Example

let firstName = "Arjun";
let lastName = "Kumar";
let isStudent = true;

Conclusion

In this chapter, you learned about the basic syntax of JavaScript, including whitespace, statements, literals, variables, operators, expressions, keywords, comments, case sensitivity, and camel case. Understanding these concepts is essential for writing and reading JavaScript code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top