...

View Full Version : js : beginner's textbook questions : data types



lse123
12-21-2007, 06:06 PM
javascript : beginner's textbook questions : data types
-----------------------------------------------------------------------
result of below statements individually(?):
typeof()
parseInt(nine)

what null defers from undefined ? null var declared but not give any value ? Or value is ="" ?
How I use javascript to write an expression to convert decimal numbers to hexadecimal numbers ?

Philip M
12-21-2007, 06:40 PM
Decimal to hexadecimal:-

http://javascript.about.com/library/blh2d.htm

result of below statements individually(?):

<script language = "javascript" type="text/javascript">

x = 123;
alert (x + " " + typeof x);
y = "456";
alert (y + " " + typeof y);
z = "nine";
alert (z + " " + typeof z);
zz = parseInt(z);
alert (zz);
var k;
alert (k);
var m = "";
alert (m + " " + typeof m);

</script>

Trinithis
12-21-2007, 07:07 PM
undefined means that the variable has had no value assigned to it:


var x;
var y = {};
alert(x); // undefined
alert(y.absentValue); // undefined


null is used in object situations. For example: Let's say that you have a function that searches for an object. What happens if that object does not exist? Return null :D

In conditional situations, null, undefined, 0, "", and false all translate to false:


var falseVals = [null, undefined, 0, "", false];
for(var i = 0; i < falseVals.length; ++i)
if(falseVals[i])
alert("true");
else
alert("false");


If you want to make sure something exactly equals one of those values, you need to use the triple equality sign operator:


alert(undefined === false);
alert(undefined === undefined);
. . .

lse123
12-21-2007, 07:11 PM
typeof()
parseInt(nine)
I try pseudo-javascript but no result ? tell me result ?

Philip M
12-21-2007, 07:14 PM
typeof()
parseInt(nine)
I try pseudo-javascript but no result ? tell me result ?

Uh? :(

Trinithis
12-21-2007, 09:20 PM
alert(typeof 4);
alert(typeof "4");
alert(typeof parseInt("34px"));

lse123
12-26-2007, 10:02 AM
"Decimal to hexadecimal:-
http://javascript.about.com/library/blh2d.htm " you said above, well,
is this beginner's function (to js: "data types" start chapter) ?
if not tell me expression to calculate this ?

A1ien51
12-26-2007, 04:16 PM
Man all of your questions sound like you want pople to do your homework. Put some effort into answering them. You ight actualy learn something by looking up things!

Eric

felgall
12-26-2007, 10:54 PM
parseInt is actually intended for converting between number bases. It takes two parameters the second of which should be a number between 2 and 36 to indicate the number base. If the second parameter is not specified then the base of the number will be worked out from the first one or two characters. If it starts with '0x' it will be assumed to be base 16 (hexadecimal - 0123456789ABCDEF), if it starts with '0' it will be assumed to be base 8 (octal - 01234567), only if it doesn't start with one of those will it be assumed to be base 10 (decimal - 0123456789).

For converting strings to numbers there are a number of better ways than using the parseInt function. http://javascript.about.com/library/blstrnum.htm shows a variety of different ways to do it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum