Javascript Fundamental: Variables

JavaScript Variables

let product;
let product;
product = 'watch';
// abbreviation
let product = 'watch';
var person = "John Doe", carName = "Volvo", price = 200;
var person = "John Doe",
carName = "Volvo",
price = 200;

The Assignment Operator

let product = 'clothes';
product = 'shoes';
alert(product); // Gives popup with shoes written on it

Defining constants

const bestseller = 'watch';
bestseller = 'clothes'; // will throw an error

Javascript Identifiers

  1. Names can contain letters, digits, underscores, and dollar signs.
  2. Names must begin with a letter
  3. Names can also begin with $ and _ (but we will not use here)
  4. Names are case sensitive (y and Y are different variables)
  5. Reserved words (like JavaScript keywords) cannot be used as names

Naming right

  1. Use human-readable names like userName or shoppingCart.
  2. Stay away from abbreviations or short names like a, b, c, unless we really know what we’re doing.
  3. Make names maximally descriptive and concise. Examples of bad names are data and value. Such names say nothing. It’s only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
  4. Agree on terms within our team and in our own minds. If a site visitor is called a “user” then we should name related variables currentUser or newUser instead of currentVisitor or newManInTown.

--

--

--

Front End Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Playing with Your Browser Urls

How to build an app 2x faster and cheaper

JavaScript Object Property Flags and Descriptors

Equa Global — How Easy Is It To Update Your Cap Table?

Data Structure: Quick Sort with JavaScript

What People Learned About Object Oriented Programming Is Wrong

Setup Rails Flash Template for Turbolinks/Turbo

https://unsplash.com/photos/PN-YnI5stdQ

Rxjs Heaven (Part3: Higher-order operators )

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ajit Singh

Ajit Singh

Front End Developer

More from Medium

Writing Cleaner Code With JavaScript Array Methods

Learn Regular Expression in JavaScript

Template Literals in JavaScript ES6

JavaScript Block Scope V/S Function Scope