hi all,
i have one question about variable in JavaScript

<HEAD>
<TITLE>Window Opener and Closer</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var newWindow
function makeNewWindow() {
newWindow = window.open("","","HEIGHT=300,WIDTH=300")
}
function closeNewWindow() {
if (newWindow) {
newWindow.close()
newWindow = null
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="button" VALUE="Create New Window" onClick="makeNewWindow()">
<INPUT TYPE="button" VALUE="Close New Window" onClick="closeNewWindow()">
</FORM>
</BODY>
</HTML>

can anyone explain why here newWindow variable equals to null.
what does mean here null

newWindow=null

Thanks in advance

why here newWindow variable equals to null.

That is a way of preventing closeNewWindow() from trying to close an already closed window.
Once the window has been closed [and newWindow has been set to null] the test if (newWindow) at the beginning of that function will not be true.

but you know without it the function also works
with that variable or without it. what difference does it make in this function
thanks for attention

Possibly the author was afraid that the browser would throw an error if .close() were invoked on an already closed window.

Maybe some of them do.

Or possibly other code is expected to test if (newWindow) to prevent trying to access elements within that window when it doesn't exist, or to control the logic flow.

Thanks very much. i also have idea. may be it wastes additional system resources
Thanks Thanks Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.