What is the var keyword in JavaScript for and when should it be used?
Hello everyone! I just started learning JavaScript and came across a misunderstanding of the `var` keyword. In some code examples, I see variables declared with `var`, while in others, they simply assign a value without it, for example:
```javascript
var name = "Anna";
age = 25;
```
Both options seem to work, and I can output the values of these variables to the console. But I've heard that if you don't use `var`, problems can arise. Can you explain what the difference is? When is it mandatory to write `var`, and when can it be omitted? And what is "hoisting" that everyone talks about? I want to write clean and safe code, but I'm still confused about these details. Thanks in advance for your help!
<<>>
Difference between declaring with var and without it
The `var` keyword in Java
No answers yet.
Log in to leave an answer
Blogs