DEV Community

Samuel Lucas
Samuel Lucas

Posted on

Creating variables in JavaScript

Variables: what are variables? A variable can be thought of as a container that stores items.

There are two types of variables, you have one which you can reassign values to and another which you can't reassign values to.

Let's see the different variables we have with examples and how they function.

1. var
✅ how to use: var name = "Lucas"
✅ function: it is a "reassignable" variable. Meaning you can do something like this in your code
var name = 'Lucas'
var name = 'Samuel'
The result is that the variable name will hold the last assigned item, meaning it will hold Samuel.

2. let
✅ how to use: let name = "Lucas"
✅ function: it is a "reassignable" variable. Meaning you can do something like this in your code
let name = 'Lucas'
let name = 'Samuel'
The result is that the variable name will hold the last assigned item, meaning it will hold Samuel.

3. const
✅ how to use: const name = "Lucas"
✅ function: it is a "non reassignable" variable. Meaning you cannot do something like this in your code
const name = 'Lucas'
const name = 'Samuel'
An error will occur that you cannot reassign values it items to name.

There's a catch to var tho' and that's the fact that it's no longer in use from the previous es6.

Why? I don't know as well 🤷, just kidding. Read it up from here to learn more. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay