• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in javascript variable declaration.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i have problem in declaring the variable in javascript.

Example :

var x=10;
alert(x);

the above declaration is working fine in javascript. but

int y=10;
alert(y);

the problem is, the alert is not coming for the declaration.

i dont understand the reson.

can any one help me.

thanks
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried on Mozilla its not working but come up with error:

Error Message.

missing ; before statement

int <right here -->y = 10;


 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because JavaScript does not have typed objects/varaiables. You only have var.

Eric
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Eric,

but in other condition I saw some javascript code like:



in that condition how JavaScript understand, i mean what kind of variable is being created in that condition and if it is String type then how it understand the Object type.

Is this use JVM to compile this code?

Thanks in Advance.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript is loose typing, variable data types not declared.

When a variable is first defined

var foo; is undefined

When it is assigned its value or object, that is when we know its type. We can reuse the variable and its true type will change.



That is why it is important in JavaScript that you are carefull on how you code things. I am an old school guy that loves to say strFoo, objFoo and intFoo when I am working with variables of known types. People really hate that notation, but I love it for JavaScript. (I still use isFoo for Booleans)

As you pointed out there are the constructors:



Just remember JavaScript is NOT a compiled language, it is interpreted at runtime. In 2.0 specs they are going to have typed variables, but I do not see that coming into existance for a long time.

Also JVM has nothing to do with JavaScript. Only thing that Java and JavaScript share is Java. (It is named JavaScript because the Netscape guys that coined it wanted to use the buzz word that was hot at the time!)

Eric
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric,

for make me clear on that..

 
reply
    Bookmark Topic Watch Topic
  • New Topic