DEV Community

vidhya murali
vidhya murali

Posted on

Javascript Variables

Hi, Let's Learn Variables in Javascript.

Variable[Variables = Data Containers]

  • A JavaScript variable is a named container used to store, reference, and manipulate data values in a program. Because JavaScript is dynamically typed, you do not need to specify the type of data (like text or numbers) when creating a variable; the engine determines it automatically at runtime.

  • Variables are identified with unique names called identifiers.

Ex : a=10 ---> a- variable , 10- value

The rules for constructing names (identifiers) are:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter, a $ sign or an underscore (_).Numbers are not allowed as the first character in names.
  • Names are case sensitive (X is different from x).
  • Reserved words (JavaScript keywords) cannot be used as names.

Dynamically Typed vs Statically Typed Languages

Statically Typed Languages

  • How it works: Variable data types are known and enforced before the program runs (at compile time).
  • Key traits: You must declare types explicitly, and errors are caught early.
  • Pros: More optimized, faster execution, fewer surprises in production.
  • Examples: C, C++, Java, Rust, Go.

Dynamically Typed Languages

  • How it works: Data types are assigned and checked during execution (at runtime).
  • Key traits: You don't need to specify variable types, and values can change types freely.
  • Pros: Faster setup, more flexible, and code is often quicker to write.
  • Examples: Python, JavaScript, Ruby, PHP.

Ways to Declare Variables Modern JavaScript uses three primary keywords to create variables:

  • const: Declares a block-scoped constant value that cannot be reassigned after initialization.
  • let: Declares a block-scoped mutable variable that can be reassigned later.
  • var: Declares a function-scoped variable. This is an older legacy keyword that is mostly avoided in modern code.

  • Variables are labels for data values.

  • Variables are containers for storing data.

Top comments (0)