JavaScript variables serve as fundamental elements in any programming language. In JavaScript, they play a crucial role in storing values that can be reused throughout the program. The assignment operator ("=") is utilized to allocate values to variables, facilitating the storage and manipulation of data within the code.
JavaScript variables must possess distinct names known as identifiers. Adhering to specific rules is imperative when declaring variables in JavaScript:
Case Sensitivity: JavaScript variables are case-sensitive, meaning that distinctions between uppercase and lowercase letters are significant.
Naming Constraints: Variable names can commence with a letter, underscore (_), or dollar sign ($). Subsequent characters can include letters, numbers, underscores, or dollar signs.
Reserved Keywords: Avoid using reserved keywords as variable names, as these terms have predefined meanings in JavaScript.
JavaScript is a dynamically typed language, which means that variable types are determined at runtime. Consequently, there is no requirement to explicitly specify the type of a variable. Variables in JavaScript can be declared in three ways.
JavaScript var keyword
JavaScript let keyword
JavaScript const keyword
Syntax:
var x = "Hello Cits" // Declaration using var
let $ = "Welcome" // Declaration using let
const _example = "Abc" // Declaration using const
All three keywords—var, let, and const—serve the fundamental purpose of declaring variables in JavaScript, albeit with some distinctions. Initially, the var keyword was the primary mechanism for variable declaration. However, with the introduction of ECMAScript 6 (ES6), the let and const keywords were introduced, offering improved scoping rules and additional features for better variable management.
Example:- In this example, we will declare variables using var.
const message = "HELLO CITSWORLD";
const number1 = 10;
const number2 = 12;
const sum = number1 + number2;
console.log(message);
console.log(number1);
console.log(number2);
console.log(sum);
Output:-
Hello CITSWORLD
10
12
22
Example 2: In this example, we will declare variables using let.
const greeting = "Hello CITS";
const joiningWord = "CITSWORLD";
const numericString = " 110";
const concatenatedString = joiningWord + numericString;
console.log(greeting);
console.log(joiningWord);
console.log(numericString);
console.log(concatenatedString);
Output:-
Hello CITS
CITSWORLD
110
CITSWORLD 110
A local variable in JavaScript is defined within a block or function and can only be accessed within that specific block or function.
Example:-
<script>
function abc() {
var x = 10; // This is a local variable
}
</script>
OR,
<script>
if (10 < 13) {
let y = 20; // JavaScript local variable using 'let'
}
</script>
A global variable in JavaScript is accessible across any function. It refers to a variable declared outside a function or explicitly associated with the window object.
<script>
var data = 200; // global variable
function displayData() {
document.writeln(data);
}
displayData(); // calling JavaScript function
displayData();
</script
Operators serve various purposes such as assigning values, comparing values, executing arithmetic operations, and fulfilling other computational tasks
There are different types of JavaScript operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Conditional Operators
Type Operators
Arithmetic operators are employed for executing mathematical operations on variables and/or values. Using the example where y is assigned the value 5, the table below elucidates the functionality of arithmetic operators
Example:-
let x = 5;
let y = 10;
console.log(x == y); // false
console.log(x === y); // false
console.log(x != y); // true
console.log(x !== y); // true
console.log(x > y); // false
console.log(x < y); // true
console.log(x >= y); // false
console.log(x <= y); // true
let condition1 = true;
let condition2 = false;
console.log(condition1 && condition2); // false
console.log(condition1 || condition2); // true
console.log(!condition1); // false
let result = (x > y) ? 'x is greater' : 'y is greater';
console.log(result); // 'y is greater'
In JavaScript, an expression is a combination of values, variables, operators, and functions that can be evaluated to produce a result. Essentially, an expression is anything that resolves to a value. Expressions can be simple or complex, and they are an integral part of programming languages for performing computations, making decisions, and creating dynamic behavior in programs.
Here are a few examples of JavaScript expressions:
1.Literal Values:
5 // Numeric literal expression
'Hello' // String literal expression
true // Boolean literal expression
2.Variables
var x = 10; // Variable assignment expression
x + 5 // Variable and arithmetic expression
3.Arithmetic Expressions:
3 + 4 // Addition expression
x * y // Multiplication expression with variables
4.Comparison Expressions:
x > 5 // Greater than expression
y <= 10 // Less than or equal to expression
5.Logical Expressions:
(x > 5) && (y <= 10) // Logical AND expression
6.Function Calls:
Math.sqrt(25) // Function call expression
7.Array and Object Expressions:
[1, 2, 3] // Array literal expression
{ name: 'John' } // Object literal expression
8.Conditional (Ternary) Operator:
var result = (x > 0) ? 'Positive' : 'Negative'; // Ternary operator expression
Example:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Syntax, Variables, Operators, and Expressions</title>
</head>
<body>
<h1>JavaScript Syntax, Variables, Operators, and Expressions</h1>
<p>This is a simple demonstration of JavaScript syntax, variables, operators, and expressions.</p>
<script>
// Variables
let num1 = 10;
let num2 = 5;
// Operators and expressions
let sum = num1 + num2;
let difference = num1 - num2;
let product = num1 * num2;
let quotient = num1 / num2;
// Output results
document.write("<p>num1: " + num1 + "</p>");
document.write("<p>num2: " + num2 + "</p>");
document.write("<p>Sum: " + sum + "</p>");
document.write("<p>Difference: " + difference + "</p>");
document.write("<p>Product: " + product + "</p>");
document.write("<p>Quotient: " + quotient + "</p>");
</script>
</body>
</html>
Output:-