Client side Java script Server side Java script Data types variables Operators Expressions Functions Objects Array Date and math related objects Document objectmodel Event handling
In JavaScript, the distinction between expressions and statements is fundamental to understanding how code executes.
Expressions:
An expression is any
piece of code that evaluates to a single value. Expressions can be
simple or complex, and they always produce a result.
Examples of
Expressions:
Literals: 5, "hello", true
Variables: x, myVariable
Arithmetic
operations:
2
+ 3, x * y
String
concatenation:
"Hello"
+ " World"
Function calls:
myFunction(), console.log("message")
Logical operations:
a
&& b, !c
Assignment expressions: x = 10 (the
expression itself evaluates to the assigned value, 10)
Key characteristics of expressions:
Always produce a value: This is the defining feature of an expression.
Can be simple or complex: They can be a single literal or a complex
combination of operators, variables, and function calls.
Can be nested: Expressions
can contain other expressions, allowing for complex computations.
Statements:
A statement is a complete
instruction that performs an action or controls the flow of execution in a
program. Statements
do not necessarily evaluate to a value, although they often contain
expressions.
Examples of
Statements:
Variable
declarations:
let
x;, const PI = 3.14;
Conditional
statements:
if
(condition) { ... } else { ... }
Loop statements:
for
(let i = 0; i < 5; i++) { ... }, while (condition) { ... }
Function
declarations:
function
greet() { ... }
Block statements:
{
console.log("inside block"); }
Expression
statements:
An
expression followed by a semicolon, like x = 5; or myFunction();
Key Differences:
Value Production: Expressions always
produce a value, while statements perform an action.
Usage: Expressions can be
used wherever a value is expected (e.g., as arguments to functions, in assignments,
within other expressions). Statements form the core instructions of a
program.
Context in Frameworks: In contexts like React's JSX, only expressions are
permitted within curly braces {} for dynamic content, not statements. (What is JSX? JSX stands for JavaScript XML.
JSX allows us to write HTML in React. JSX makes it easier to write and add HTML
in React, React lets you build user interfaces out of individual pieces
called components. Create your own React components like Thumbnail,
LikeButton, and Video.)
Relationship:
Expressions
are often components of statements. For instance, in if (x > 5) {
... }, x > 5 is an expression that evaluates to a boolean value,
and the entire if construct is a statement. Similarly, let
result = a + b; involves the expression a + b within a variable
declaration statement.
JavaScript Statements
Example
let x,
y, z; // Statement 1
x = 5; //
Statement 2
y = 6; //
Statement 3
z = x + y; // Statement 4
JavaScript statements are composed of:
Values,
Operators, Expressions, Keywords, and Comments.
This
statement tells the browser to write "Hello Dolly." inside an HTML
element with id="demo":
Example
document.getElementById("demo").innerHTML = "Hello
Dolly.";
Semicolons ;
Semicolons
separate JavaScript statements.
Add
a semicolon at the end of each executable statement:
Examples
let a,
b, c; // Declare 3 variables
a = 5; // Assign the value
5 to a
b = 6; // Assign the value
6 to b
c = a + b; // Assign the sum of a and b to c
When
separated by semicolons, multiple statements on one line are allowed:
a
= 5; b = 6; c = a + b;
JavaScript Keywords
JavaScript
statements often start with a keyword to identify the JavaScript
action to be performed.
|
Keyword |
Description |
|
var |
Declares
a variable |
|
let |
Declares
a block variable |
|
const |
Declares
a block constant |
|
if |
Marks
a block of statements to be executed on a condition |
|
switch |
Marks
a block of statements to be executed in different cases |
|
for |
Marks
a block of statements to be executed in a loop |
|
function |
Declares
a function |
|
return |
Exits
a function |
|
try |
Implements
error handling to a block of statements |
Note
JavaScript
keywords are reserved words. Reserved words cannot be used as names for
variables.
No comments:
Post a Comment