JavaScript empty statement is used to provide no statement, although the JavaScript syntax would expect one. The empty statement is a semicolon (;
) indicating that no statement will be executed, even if JavaScript syntax requires one.
;
JavaScript empty statement
Simple example code empty loop body:
<!DOCTYPE html>
<html>
<body>
<script >
let arr = [1, 2, 3];
// Assign all array values to 0
for (let i = 0; i < arr.length; arr[i++] = 0)
/* empty statement */ ;
console.log(arr);
</script>
</body>
</html>
Output:
If you are looking for JavaScript empty() method then you have to use jQuery empty() Method. The empty() method removes all child nodes and content from the selected elements.
$(selector).empty()
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").empty();
});
});
</script>
</head>
<body>
<div style="height:100px;background-color:yellow">
This is some text
<p>This is a paragraph inside the div.</p>
</div>
<p>This is a paragraph outside the div.</p>
<button>Remove content of the div element</button>
</body>
</html>
What is the point of using empty statements in JavaScript?
Answer: The examples you’ve given don’t make much sense. They should better be written
for (var i = 0; i < a.length;) a[i++] = 0;
for (var i = 0; i < a.length; i++) a[i] = 0;
; // the comparisons really don't do anything (assuming a and b are no objects)
(a==0) || (b = 0); // Oh wait, that's the one given by @Shomz
if (a != 0) b = 0;
However, there are real-world applications for the empty statement. I’ll just list 3 that come to my mind:
function x() { … };
A semicolon where it doesn’t belong (e.g. after the function declaration above) makes an empty statement.; …
A leading semicolon on your script files helps to guard against buggy inclusions or file concatenations.while (!check_for_finish()); // do nothing
An empty loop body can be used for busy-waiting loops (not recommended) and similar.
Source: stackoverflow.com
Do comment if you have any doubts or suggestions on this Js Basic tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version