• 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

variable declaration in javascript

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just started learning javascript.

I am trying to use something like:



inside script tag in a html file.

But I am getting error, however if I use only

it works fine.

Does it mean that it is illegal to provide type of the variable while declaring it in javascirpt?

Regards,
Joshua
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by joshua antony:
Does it mean that it is illegal to provide type of the variable while declaring it in javascirpt?


Yes. JavaScript is an untyped, interpretive language.

The correct syntax is:


If you omit var at the top level, the variable becomes a property of window.
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear,

One more query,

Say I have the below code;



where route is another object and returns an object of type GDistanceDuration.

GDistanceDuration has a method called getDays().

but ,if I invoke duration.getDays() then I am getting error that
getDays() is not valid for duration object.

And I try to print the runtime object using
alert(typeof duration); and to my surprise, it is displaying as 'Object'.
I had expected GDistanceDuration.

So can't the script recognize that the runtime object is of GDistanceDuration ?

Regards,
Joshua
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the duration object truly has a method named getDays(), then it will be called without the error. You may be misinterpreting how to use the API.

All objects are of type 'object'. JavaScript is not Java and there are no true object types in the way that we think about them in Java. They're just fancily decorated instances of Object.
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear,

It really helped.

Regards,
Joshua
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic