These are the JavaScript Statements Everyone Should Know

debugger output

Hello friends today we gonna learn about These are the JavaScript Statements Everyone Should Know. So read this article, These are the JavaScript Statements Everyone Should Know step by step so you may understand it in a better way. If you have query related to same article you may contact us. So Lets begin:

Guide: These are the JavaScript Statements Everyone Should Know

JavaScript instructions allow us to use implement different kinds of logic in our code. JavaScript offers us several, each of which has its own purpose and syntax. Among the most well-known examples, we can find expression statements, iteration statements, conditional statements, and more

We will see in today’s post four less common JavaScript statements that you may not have known before, but you can deepen your JavaScript knowledge and enable you to write better code.

back to menu ↑

1. Empty overview

Instead of a JavaScript statement, you can add an empty statement, which is written as a single semicolon When the JavaScript interpreter interprets an empty statement, no code is executed, therefore they can be useful to replace sub statements that you do not want to be executed

Suppose there is a variable called litmus with the default value of neutral. Based on the value of another variable called pH, litmus turns into acid as pH <7 of basisch als pH> 7.

If the pH value is found to be invalid, an error is generated. For a condition like this, the following conditional statements apply:

var litmus = “neutral”; var pH;

if (pH> 0 && pH <7) lakmoes = ”zuur”; else if (pH> 7 && pH <15) litmus = "basic"; elsethrow “Invalid pH value”;

However, the above set of statements will generate an error when the pH value is 7, which should not be the case.

If the pH is 7, litmus should keep its default value, that is, neutral. So for a case like this add a condition if the pH is 7 with an empty statement.

var litmus = “neutral”; var pH;

if (pH> 0 && pH <7) lakmoes = "zuur"; else if (pH === 7); / * lege verklaring * / else if (pH> 7 && pH <15) litmus = "basic"; elsethrow “Invalid pH value”;

Now, if the pH is 7, the interpreter is not executing instructions, and litmus keeps its default value, neutral.

Empty statements can also be used to populate an array using the for loop.

var ary = []

for (var i = 0; i <5; ary[i++] = i); / * empty instruction * / console.log (ary); // [1, 2, 3, 4, 5]

Typically, a for loop statement is followed by a sub statement that is created up of a single statement or a block statement (the one enclosed in {} curly brackets) to be executed. By using an empty statement instead of the sub statement, the interpreter doesn’t have to execute after each loop, so only the looping takes place and the loop conditions are executed

In the example above, ary[i++] = i is executed for each loop iteration as part of the loop condition, and the array is instantiated with values ​​of i.

back to menu ↑

2. The debugger statement

In debugging programs, we can add markers breakpoints named after each line in the source code to mark the lines from which the debugger tool starts debugging

In JavaScript, the debugger statement is works in the same way as a breakpoint, except that it added directly in the source code, rather than within a tool. Any active debugger will stop the script execution when it reaches the debugger statement to help you debug the code.

Remember that debugging is activated only if the script is running in debug modeie, a debugger is already running on script execution. If no debugger program is currently running while interpreting the debugger statement, the interpreter will continue working as if nothing had happened.

As a quick test, run the following code in Codepen, while keeping the browser’s debugger tool open:

console.log (‘tesing’); debugger; console.log (‘debugging statement’);

You will see a breakpoint next to the debug statement, as shown below in the browser’s debugging tool.

back to menu ↑

3. Labeled statement

In JavaScript, you can also add labels to certain statements. By doing this, you can later jump to the labeled instruction by using the label in your code, much like the goto statement works in some other languages.

Labeled statements can only be used in conjunction with the break and continue statements, as there is no literal goto statement in JavaScript.

Both break and continue can only be used in loop statements, such as the for loop (with one exception, break can also be used in the switch statement). So we can label and break loops and continue to control their execution.

The syntax of labeled statements is simple, you just need to add the name of the label with a subsequent colon, as you can see in the example below, where loop is the name of the label we are adding to the for loop.

loop: for (var i = 0; i <5; i ++) {if (i === 2) continue loop; console.log (i); // 0, 1, 3, 4}

If the value of i is 2, the execution jumps back to the loop instead of continuing, avoiding the console output of “2”.

Now let’s consider another example with the break statement. Just replace the continuous keyword with break in the example above, and you’ll notice that instead of jumping back to the loop like with continue, the loop ends / breaks all the way.

loop: for (var i = 0; i <5; i ++) {if (i === 2) break loop; console.log (i); // 0, 1}

The above examples were quite simple so you can quickly understand how labeled statements work, but in real coding, labels are more commonly used in compound loops when it is necessary to distinguish the different loops, as in the following example:

loop: for (var i = 0; i <4; i ++) {for (var j = 0; j <2; j ++) {if (i === 2 && j === 1) break loop ; console.log (i + "-" + j);}

here the outer loop breaks at value 2 for variable i and at 1 for j, and the console returns the following output:

0-00-11-01-12-0

back to menu ↑

4. The with statement

Then the JS interpreter encounters an unqualified name which does not indicate which object or function the call is associated with, it searches the scope chain for any suitable object or function that the call can refer to.

Using the met statement, we can add an object at the top of the scope chain, and specify which object the call is associated with.

In the following example, you can see that the properties of the person object are called with only their name within the with statement.

var person = {first name: “John”, last name: “Doe”, age: “18”, country: “Greenland”};

with (person) {console.log (“Hello, my name is” + first name + “” + last name + “. I am” + age + “years old, living in” + country + “.”);} / / “Hello, my name is John Doe. I am 18 years old and I live in Greenland. “

Compare what the above code would look like without using the with statement:

var person = {first name: “John”, last name: “Doe”, age: “18”, country: “Greenland”};

console.log (“Hello, my name is” + person.firstName + “” + person.lastName + “. I am” + person.age + “years old, living in” + person.country + “.”) ; // “Hello, my name is John Doe. I am 18 years old and I live in Greenland. “

You can see that the with statement can be a great shortcut when working with many properties of the same object.

Note, however, that using the with statement in strict mode is not allowedas it may cause some confusion.

It is also recommended to use the with statement only if its internal statements use the object associated with the with statement, otherwise the interpreter will waste time examining the object mentioned by first, for any unqualified property names which he later in the with block.

back to menu ↑

These are the JavaScript Statements Everyone Should Know: benefits

  • The These are the JavaScript Statements Everyone Should Know tutorial is free .
  • This guide already helps so many users follow up with interest in a timely manner.
  • The price of the These are the JavaScript Statements Everyone Should Know guide is free.

back to menu ↑

Faq

Tutorial summary of These are the JavaScript Statements Everyone Should Know

In this guide, we told you about the These are the JavaScript Statements Everyone Should Know; please read all steps so that you understand These are the JavaScript Statements Everyone Should Know in case if you need any assistance from us, then contact us.

How this tutorial helping you?

So in this guide, we discuss the These are the JavaScript Statements Everyone Should Know, which undoubtedly helps you.

What is actual time in which this method complete?

The time to complete the These are the JavaScript Statements Everyone Should Know tutorial is 10+ minutes.

What are the supported Device?

PC Laptop or Desktop


back to menu ↑

Final note

I hope you like the guide These are the JavaScript Statements Everyone Should Know. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

For our visitors: If you have any queries regards the These are the JavaScript Statements Everyone Should Know, then please ask us through the comment section below or directly contact us.
Education: This guide or tutorial is just for educational purposes.
Misinformation: If you want to correct any misinformation about the guide “These are the JavaScript Statements Everyone Should Know”, then kindly contact us.
Want to add an alternate method: If anyone wants to add more methods to the guide These are the JavaScript Statements Everyone Should Know, then kindly contact us.
Our Contact: Kindly use our contact page regards any help. You may also use our social and accounts by following us on Whatsapp, Facebook, and Twitter for your questions. We always love to help you. We answer your questions within 24-48 hours (Weekend off).
Channel: If you want the latest software updates and discussion about any software in your pocket, then here is our Telegram channel.

Compsmag
Logo
Shopping cart