Q. What is JavaScript?
A. JavaScript is a scripting language designed for adding interactivity to HTML pages. JavaScript is an interpreted language. This means that scripts execute without preliminary compilation, i.e. without conversion of the script text into a system-dependent machine code. The user's browser interprets the script, that is, analyzes and immediately executes it.
Thus, most Internet users today have browsers that support JavaScript. That's why JavaScript is one of the most popular tools for adding interactive features to Web pages.
Q: How do I insert comments in JavaScript code?
A: JavaScript supports three different types of comments:

1. Multiple-line C-style comments. Everything between /* and */ is a comment,
for example:
/* This is a comment */
/* C-style comments can span
as many lines as you like,
as shown in this example */

2. One-line comments of C++ style. These comments begin with // and continue up to the next line break:
// This is a one-line comment

3. One-line comments with the HTML comment-opening sequence (). Consider this example:
This is also a one-line JS comment
because JS ignores the closing characters
of HTML-style comments

Q: How do I hide JS code from old browsers that do not support JavaScript?
A: To prevent old browsers from displaying your JS code, do the following:

1) Immediately after the opening

Thus, your HTML file will contain the following fragment:


Q: If the user's browser cannot execute JavaScript code, can I display a warning for the user?
A: Yes, you can display a special warning for users of JavaScript-incapable browsers. Put your warning text between the tags and .
Here's an example:


This page uses JavaScript


  • Please use Netscape Navigator 3+ or Internet Explorer 3+
  • Make sure that JavaScript is enabled in your browser.



JavaScript-enabled browsers will ignore everything between and . Browsers that cannot execute JavaScript will display your message on the page.

Q: Can I include JavaScript code from external JS files, rather than embedding all my scripts within HTML pages?
Answer: Yes. To do so, create a new file with the extension .js, for example, myscript.js. Put your JavaScript code in this file; do not include opening and closing SCRIPT tags in the .js file!

To embed myscript.js into your Web page, use these tags in your HTML file:



Q: Can I make a button on my page work as the browser's Back button?
A: To create your own Back button, use this code



Q: Can I make a button on my page work as the browser's Forward button?
A: To create your own Forward button, use this code



If your browser's Forward button is currently inactive, then the Forward button on your page won't work either. This is the case when the current page is the last page in your browsing history.

Q: Can I pass parameters from one my page to another?
Answer: Yes, you can pass a parameter to another page in several different ways:

by storing the parameter in a cookie
by storing the parameter in a variable of another window or frame
by storing the parameter in the rewritable property top.name (the browser window name)
by appending the parameter to the destination page's URL as a query string

Q: How do I convert strings to numbers in JavaScript?
Answer: To convert a string to a number, use the JavaScript function parseFloat (for conversion to a floating-point number) or parseInt (for conversion to an integer).

parseFloat syntax: parseFloat('string')

Q: Is there a way to test whether a particular variable holds a number or a string?
Answer: Yes. To test whether the variable holds a number or a string, use the typeof operator. If your variable holds a number, typeof(variable) will return "number". If it holds a string, typeof(variable) will return "string". The following are examples of typeof usage:

typeof(123) // result: "number"
typeof("123") // result: "string"

if (typeof k == "string") { alert('k is a string.') }
if (typeof k == "number") { alert('k is a number.') }

Q: How do I generate random numbers in JavaScript?
Answer: To generate random floating-point numbers in the range from 0 to 1, use the Math.random() method:

num = Math.random() // num is random, from 0 to 1

If you need random floating-point numbers in the range from A to B, use this code:
num = A + (B-A)*Math.random() // num is random, from A to B

Q: How do I extract a substring from a string?
Answer: To extract a substring from a string, use the substring method:

string.substring(start,end)

Here
string - is the string from which you want to extract a substring.
start - is the number specifying the position of the character at which the substring begins. (The character at start itself will be included in the substring.)
end - is the number specifying the position of the character at which the substring ends. (The character at end will not be included in the substring.)

Note that the first character in the string corresponds to position 0, and the last character to position string.length-1.

Examples:

'Hello'.substring(0,2) // 'He'
'Hello'.substring(0,4) // 'Hell'

Q: Can I play a sound file without using JavaScript?
A: Yes, you can play a sound by specifying the sound file's URL as a hyperlink destination, for example,