Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript variable declaration not understand, anyone explain?

I see many project and many code, all code understand properly but in code one thing i don’t understand to declare variable, so please describe the actually why it declare ?

var query = {};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In javascript, a variable can store all types of data without declaration. But when there is a need to store data in form of key-value pairs, it’s necessary to define an object scope and then assign values using keys.

Example 1 (Correct way)

var a = {};
a.kwy1 = 10;
a.kwy2 = "Ten";
console.log(a);

Example 2 (incorrect)

var b; //here though b is declared, its not initialised with object scope. so it will give an error - Uncaught ReferenceError: b is not defined
b.kwy1 = 10;
b.kwy2 = "Ten";
console.log(b);

Example 3 (incorrect)

//here though c is not declared so will give an error -  Uncaught ReferenceError: c is not defined
c.kwy1 = 10;
c.kwy2 = "Ten";
console.log(c);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading