|
Javascript Interview Questions and Answers
How to convert a string to a number using
JavaScript?
You can use the parseInt() and parseFloat() methods. Notice that extra
letters following a valid number are ignored, which is kinda wierd but
convenient at times.
parseInt("100") ==> 100
parseFloat("98.6") ==> 98.6
parseFloat("98.6 is a common temperature.") ==> 98.6
parseInt("aa") ==> Nan //Not a Number
parseInt("aa",16) ==> 170 //you can supply a radix or base
How to convert numbers to strings using JavaScript?
You can prepend the number with an empty string
var mystring = ""+myinteger;
or
var mystring = myinteger.toString();
You can specify a base for the conversion,
var myinteger = 14;
var mystring = myinteger.toString(16);
mystring will be "e".
How to test for bad numbers using JavaScript?
the global method, "isNaN()" can tell if a number has gone bad.
var temperature = parseFloat(myTemperatureWidget.value);
if(!isNaN(temperature)) {
alert("Please enter a valid temperature.");
}
What's Math Constants and Functions using JavaScript?
The Math object contains useful constants such as Math.PI, Math.E
Math also has a zillion helpful functions.
Math.abs(value); //absolute value
Math.max(value1, value2); //find the largest
Math.random() //generate a decimal number between 0 and 1
Math.floor(Math.random()*101) //generate a decimal number between 0 and
100
What's the Date object using JavaScript?
Time inside a date object is stored as milliseconds since Jan 1, 1970.
new Date(06,01,02) // produces "Fri Feb 02 1906 00:00:00 GMT-0600
(Central Standard Time)"
new Date(06,01,02).toLocaleString() // produces "Friday, February 02,
1906 00:00:00"
new Date(06,01,02) - new Date(06,01,01) // produces "86400000"
What does the delete operator do?
The delete operator is used to delete all the variables and objects used
in the program ,but it does not delete variables declared with var
keyword.
How tp create Arrays using JavaScript ?
<script type="text/javascript">
var days = new Array();
days[0] = "Sunday"
days[1] = "Monday"
days[2] = "Tuesday"
days[3] = "Wednesday"
days[4] = "Thursday"
days[5] = "Friday"
days[6] = "Saturday"
document.write("first day is "+days[0])
</script>
This produces
first day is Sunday
A more compact way of creating an array is the literal notation:
<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("first day is "+days[0]) </script>
This produces
first day is Sunday
How to delete an entry using JavaScript?
The "delete" operator removes an array element, but oddly does not
change the size of the array.
<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("Number of days:"+days.length); delete days[4];
document.write("<br />Number of days:"+days.length);
</script>
This produces
Number of days:7
Number of days:7
Page Numbers
:
1
2
3
4
5
6
7
8
9
10
11
12
13
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
|