Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are Variables, Data Types and Operators in JavaScript?

Web DevelopmentCSSJavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
7 min read

In JavaScript, variables store values, data types describe what kind of value that is (number, string, boolean and others), and operators combine or compare values. You declare variables with let and const, and the language figures out the type of each value while the program runs. This guide on javascript variables data types is aimed at beginners, so every term is explained with a small example.

Modern JavaScript code uses const by default, let when a value must change, and avoids the older var.

Table of Contents

  • Declaring variables with const, let and var
  • The primitive data types
  • Working with strings and template literals
  • Arithmetic, assignment and comparison operators
  • Logical operators, truthy values and nullish checks
  • Type conversion and common pitfalls
  • Frequently Asked Questions
    • What is the difference between let and const?
    • Why does typeof null return object?
    • What is the difference between null and undefined?
    • Should I still use var?
  • Summary
  • Related Articles

Declaring variables with const, let and var

A variable is a named container for a value. JavaScript has three ways to declare one.

javascript
javascript
Loading syntax highlighting...
text
KeywordReassign?ScopeUse today?
constNoBlockYes, by default
letYesBlockYes, when the value changes
varYesFunctionAvoid in new code

Block scope means a variable exists only inside the nearest pair of curly braces. Note that const stops reassignment of the variable, but an object or array held by a const can still be changed internally.

Names are case sensitive, can contain letters, digits, _ and $, and cannot start with a digit. By convention, use camelCase such as firstName.

The primitive data types

JavaScript has seven primitive types and one structured type.

TypeExampleNotes
number42, 3.14Integers and decimals, 64-bit floating point
bigint9007199254740993nIntegers of any size
string"hello", 'hi', `hi`Text
booleantrue, falseLogic values
undefinedundefinedA variable with no value yet
nullnullIntentional empty value
symbolSymbol("id")Unique identifiers
object{ a: 1 }, [1, 2]Objects, arrays, functions and more

Use typeof to check a value:

javascript
javascript
Loading syntax highlighting...

typeof null returns "object", which is a long-standing quirk of the language, so compare with === null instead. Because all numbers are floating point, 0.1 + 0.2 gives 0.30000000000000004; for money, store whole cents as integers.

Working with strings and template literals

Strings can use single quotes, double quotes or backticks. Backticks create template literals, which can span lines and embed expressions.

javascript
javascript
Loading syntax highlighting...
text

Strings are immutable: methods such as toUpperCase() return a new string rather than changing the original.

Arithmetic, assignment and comparison operators

Operators work on values, called operands.

javascript
javascript
Loading syntax highlighting...

Comparison operators return booleans. Prefer strict equality.

javascript
javascript
Loading syntax highlighting...

=== compares value and type, while == converts types first, which causes surprises. Use === and !== almost always.

Logical operators, truthy values and nullish checks

The logical operators are && (and), || (or) and ! (not). They work on truthy and falsy values: the falsy values are false, 0, "", null, undefined, NaN and 0n; everything else is truthy.

javascript
javascript
Loading syntax highlighting...

The nullish coalescing operator ?? gives a default only for null or undefined, which is safer than || when 0 or an empty string is a valid value. Optional chaining ?. stops safely when part of a path is missing.

Type conversion and common pitfalls

JavaScript converts types automatically in some cases, which is called coercion.

javascript
javascript
Loading syntax highlighting...

To avoid surprises:

  • Convert explicitly with Number(), String() and Boolean().
  • Check for NaN with Number.isNaN(value), because NaN === NaN is false.
  • Use === instead of ==.
  • Declare variables before use; accessing a let or const before its declaration throws an error.

Frequently Asked Questions

What is the difference between let and const?

Both are block scoped. let allows reassignment, while const does not. Use const unless you know the value must change.

Why does typeof null return object?

It is a bug from the first version of JavaScript that cannot be fixed without breaking existing sites. Check for null with value === null.

What is the difference between null and undefined?

undefined means a value has not been assigned, while null is an intentional empty value that you set yourself.

Should I still use var?

No. var is function scoped and hoisted in confusing ways. let and const are clearer and safer in modern code.

Summary

Declare variables with const by default and let when they change. Know the primitive types (number, bigint, string, boolean, undefined, null, symbol) and objects, use typeof carefully, and prefer template literals for text. Use === for comparison, ?? and ?. for missing values, and convert types explicitly to avoid coercion surprises.

Related Articles

  • What Is JavaScript and What Role Does It Play on the Web?
  • How Do Functions and Scope Work in JavaScript?
  • Learn Web Development: Complete Roadmap for Beginners
Share

Similar posts

  • How Do You Fetch Data from an API with the Fetch API?
  • What Are Promises and Async Await in JavaScript?
  • How Do Events and Event Listeners Work in JavaScript?
  • What Is the DOM and How Do You Change a Page with JavaScript?
  • How Do Functions and Scope Work in JavaScript?
  • What Is JavaScript and What Role Does It Play on the Web?
  • How to Start Web Development: A Beginner Roadmap
  • How Do You Make a Website Accessible for All Users?