JavaScript – Beginning Interview Questions

In doing numerous technical interviews of Internet Application Developers, I’ve discovered that most of these candidates consider their JavaScript skills to be at an “expert” level or they feel their JavaScript skills are “very strong.” In the course of the interview, however, it is often discovered that the candidates only have a cursory understanding [...]

How to Add an Option to a Dropdown Select Box with JavaScript

To add an option to a dropdown select box with JavaScript, first create the option that will be added. Then add the option, as shown in the following example.
If this is the select list:
<select name='dropdown' id='dropdown'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
This JavaScript will add an option at the end of the list with a value of 3 and [...]

How to Remove an Option from a Dropdown Select Box with JavaScript

To remove an option from a dropdown select box with JavaScript, use the JavaScript remove method. Pass it the index of the option that should be removed. The index is 0-based, so to remove the first item in the select list pass 0, to remove the second pass 1 and so forth.
If this is the [...]

How to Create a Function in JavaScript

A function can be created in JavaScript by using the function statement, as follows:
function CalculateTotal(intInput)
{
  var intTotal = 0;
  intTotal = intInput*2;
  return intTotal;
}
To call this function in JavaScript code, simply do this:
CalculateTotal(4);
The curly braces are required as part of the function statement.

How to Remove All Options from a Dropdown Select Box with JavaScript

To remove all options from a dropdown select box with JavaScript, simply set the length property of the dropdown box to 0. For instance, if a dropdown select box is defined as follows:
<select name='Dropdown' id='Dropdown'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
Use this JavaScript to remove all the options in the dropdown select box.
document.getElementById('Dropdown').length = 0;

How to Remove All Elements From a JavaScript Array

To remove all the elements from a JavaScript array, simply set the array length to 0, as shown in the following example:
var aryTest = [1,2,3,4];
document.write(aryTest.length);
aryTest.length = 0;
document.write(aryTest.length);
Related articles:
JavaScript Arrays Summary and Reference
JavaScript Array length Property

JavaScript to Get Number of Days in a Month

At work recently the need came up to determine the number of days in a month for a given month and year. This can be accomplished by creating a date object for the given month and year. Then do a while loop. In each iteration of the loop, set the day of [...]

JavaScript to Replace Backslash

Backslashes can be removed from a JavaScript string as follows:
var strBackslash = ‘\\backslash’;
var strReplace = strBackslash.replace(/\\/,”);
document.write(strReplace);
Related articles:
JavaScript Regular Expressions

How to Include an External JavaScript File

To include an external JavaScript file in a file, use the script tag.
<script src='../filename.js'></script>
The src attribute refers to the path where the included file resides. The file specified is a file that contains JavaScript only and has no script tags.
Note that the end script tag immediately follows the beginning script tag.
Why use the script tag [...]

How to Determine the Operating System of the Client Machine with JavaScript

To determine the operating system of the client machine on which a browser is executing JavaScript, use the appVersion property of the JavaScript navigator object, as demonstrated in the following example:
document.write(navigator.appVersion);
The information given in this property can be extrapolated, albeit in a somewhat rudimentary way, to determine whether the operating system is Windows or Macintosh, [...]