![]() | ㅤ | Tamim Ahmed17 Mar 2023 (1 week ago)
|

In JavaScript, a statement is a unit of code that performs an action or a set of actions. Statements are executed in the order in which they appear in the code.
Here are some examples of JavaScript statements:
let x = 5;
Function call statement: calls a function to perform a task
console.log("Hello, world!");
Conditional statement: executes different code based on a condition
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}
Loop statement: executes a block of code repeatedly
for (let i = 0; i < 5; i++) {
console.log(i);
}
Jump statement: changes the control flow of the program by jumping to a different part of the code
return true;
These statements can be combined to create more complex programs. For example, you can use conditional statements and loops to create programs that make decisions and repeat tasks based on certain conditions. You can also use functions to organize your code into reusable units.
It’s important to remember that JavaScript statements must be terminated with a semicolon (;) to indicate the end of the statement. While some statements may work without a semicolon, it’s best to include them to avoid potential errors.
JavaScript White Space
In JavaScript, white space refers to any non-printable characters, such as spaces, tabs, and line breaks, that are used to format code for readability. JavaScript ignores white space between statements and operators, which means that you can add extra spaces and line breaks to your code without affecting its behavior.
For example, the following two code snippets are equivalent:
let x=5;
let y=10;
let z=x+y;
Both snippets define three variables, assign values to x
and y
, and calculate the sum of x
and y
and store it in z
. The only difference is the amount of white space used to format the code.
However, white space can affect the behavior of certain statements, such as string literals, regular expressions, and template literals, which preserve white space and line breaks.
For example, the following code creates a string literal with a line break:
let str = "Hello,\nworld!";
console.log(str);
When the console.log()
function displays the str
variable, it preserves the line break and displays the message on two lines:
Hello,
world!
Similarly, the following code creates a template literal with white space:
let name = "John";
let age = 30;
let message = `My name is ${name} and I'm ${age} years old.`;
console.log(message);
When the console.log()
function displays the message
variable, it preserves the white space in the template literal and displays the message with the correct formatting:
My name is John and I'm 30 years old.
In general, it’s a good practice to use white space to format your code for readability, but be aware of its potential effects on certain statements.
JavaScript Line Length and Line Breaks
In JavaScript, line length and line breaks can affect the readability and maintainability of your code. Here are some best practices to consider:
Limit line length: Long lines of code can be difficult to read and understand, especially when they require horizontal scrolling. A good rule of thumb is to limit line length to around 80-100 characters. You can break up long lines into multiple lines by using the line continuation character \
or by using string concatenation.
let longString = "This is a very long string that " +
"spans multiple lines using " +
"string concatenation.";
Use line breaks to improve readability: Breaking up your code into smaller, more manageable chunks can make it easier to read and understand. Use line breaks to separate blocks of code, such as functions, loops, and conditional statements. Additionally, you can use line breaks to group related statements together and to indicate logical breaks in your code.
function calculateSum(a, b, c) {
let sum = a +
b +
c;
return sum;
}
Be consistent: Consistency in your line length and line breaks can make your code easier to read and maintain. Choose a line length and line break style that works for you and your team, and stick to it throughout your codebase.
// Inconsistent line breaks
let x = 5;
if (x > 10) {
console.log("x is greater than 10.");
}
// Consistent line breaks
let y = 10;
if (y > 20) {
console.log("y is greater than 20.");
}
In general, it’s a good practice to keep your code organized and easy to read, which can help you and other developers understand the code and make changes more easily.
JavaScript Code Blocks
In JavaScript, code blocks are used to group statements together into a single unit of code. A code block is defined by a pair of curly braces {}
and can contain zero or more statements.
Here’s an example of a code block:
if (x > 5) {
console.log("x is greater than 5");
x = x + 1;
}
In this example, the code block contains an if
statement that checks whether the value of the variable x
is greater than 5. If the condition is true, the code block executes two statements: it logs a message to the console and increments the value of x
by 1.
Code blocks can be used with many JavaScript statements, such as loops, functions, and conditional statements. Here are some examples:
- Loops:
for (let i = 0; i < 10; i++) {
console.log(i);
}
2. Functions:
function calculateSum(a, b, c) {
let sum = a + b + c;
return sum;
}
3. Conditional statements:
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is less than or equal to 5");
}
In general, code blocks are a powerful and versatile tool that can help you organize and structure your code in a logical way. By grouping related statements together, you can make your code easier to read, understand, and maintain.
JavaScript Keywords
In JavaScript, keywords are reserved words that have special meaning and are used to define the structure and logic of your code. Keywords cannot be used as variable or function names, as they are already used by the JavaScript language.
Here is a list of some of the keywords in JavaScript:
break
: Used to exit a loop or switch statement.case
: Used to define a case in a switch statement.catch
: Used to handle errors in a try-catch block.const
: Used to define a variable with a constant value.continue
: Used to skip to the next iteration of a loop.debugger
: Used to pause the execution of your code and allow you to debug it.default
: Used in a switch statement to define the default case.delete
: Used to delete a property from an object.do
: Used to define a do-while loop.else
: Used in an if-else statement to define the else clause.export
: Used to export a function, object, or value from a module.extends
: Used to create a subclass of a class.false
: A Boolean value that represents false.finally
: Used to define a block of code to be executed after a try-catch block.for
: Used to define a for loop.function
: Used to define a function.if
: Used to define an if statement.import
: Used to import a function, object, or value from a module.in
: Used to check if a property exists in an object.instanceof
: Used to check if an object is an instance of a particular class.let
: Used to define a variable with block scope.new
: Used to create a new instance of a class.null
: A special value that represents a null or non-existent object.return
: Used to return a value from a function.super
: Used to call a method in the parent class.switch
: Used to define a switch statement.this
: Used to refer to the current object.throw
: Used to throw an error.true
: A Boolean value that represents true.try
: Used to define a try-catch block.typeof
: Used to get the data type of a value.var
: Used to define a variable with function or global scope.void
: Used to specify that a function does not return a value.while
: Used to define a while loop.with
: Used to create a new scope with an object.
It’s important to be familiar with these keywords and their usage in order to write effective JavaScript code.
![]() |
JavaScript Syntax
1 week ago -1 likes - 24 views - No comment |
![]() |
JavaScript Tutorial
1 week ago -4 likes - 55 views - No comment |
![]() |
Computer Basics of C Programing
10 months ago -7 likes - 181 views - No comment |
No comments to “JavaScript Statements” |