Hello readers, Today in my blog I will discuss about some useful statements that are used in JavaScript.
Basically the web developers use statements as it allow us to implement different type of logic in our code.
The various statements that are used in JavaScript are expression statement, iteration statements, conditional statement and many more.
But let us discuss about some less common used statements in JavaScript and can use them in our code.
So, here are some statements these are as follows:-
- Empty Statement :- An Empty Statement is written as a single semi-colon ( ;). Whenever a JavaScript interpreter executes an empty statement no code gets executed and hence it is useful to replace sub-statements that the programmer does not want to execute.
Below is the example showing the use of Empty Statement within a code :-
var ary = [];
for(var i = 0; i < 5; ary[i++] = i)
; /* empty statement */
console.log(ary);
// [1, 2, 3, 4, 5]
As in this example, the for loop is followed by an empty statement where the interpreter have nothing to execute after each loop so therefore only the looping will occur and only the condition gets executed.
- Debugger Statement :- The Debugger Statement works same as a breakpoints that are used in Debugger tools.
It can be added into the source code directly rather than within a debugging tool.
The debugging gets triggered if a debugging program is running over the execution of the script.
Below is the code showing the Debugging Statement :-
console.log('tesing');
debugger;
console.log('debugging statement');
- Labeled Statement :- The Labeled Statements are used with the continue and break statements.
Both the break and continue statement are used within the loop and are used to control the execution.
The syntax for labeled statement is comprises of name of the label with the following colon.
Below is an example showing the use of labeled statement within a code :-
loop: for(var i=0; i<5; i++){
if(i===2)
continue loop;
console.log(i);
// 0, 1
}
As in this code, Loop is the name of the label this example shows that when the value of i gets equal to 2 the execution returns back to the loop instead of proceeding and therefore prevents the console output of “2”.
- With Statement:- The With Statement can add an object to the top of the scope chain as when a JavaScript interpreter comes across an unqualified name which is not associated to any object or function then it starts searching the scope chain with it could refer.
Below in the example the properties of the person object are called using their names alone inside the with statement.
var person = {
firstName: "John",
lastName: "Doe",
age: "18",
country: "Greenland"
};
with(person) {
console.log("Hi, my name is " + firstName + " " + lastName +
". I'm " + age + " years old, and live in " + country + ".");
}
// "Hi, my name is John Doe. I'm 18 years old, and live in Greenland."
Conclusion :-
Hence, I hope we have a brief description about some less common statements used in JavaScript.
0 Comment(s)