DEV Community

G Gokul
G Gokul

Posted on

JAVASCRIPT AND THEIR VARIABLES

Javascript:

  • JavaScript is a scripting language primarily used to create dynamic and interactive features on web pages.
  • It works alongside HTML (for structure) and CSS (for styling) to enhance user experiences by enabling functionalities like animations, form validations, and real-time updates.
  • JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic.
  • JavaScript can also able to change or update HTML and CSS dynamically.
  • JavaScript can also run on servers using tools like Node.js, allowing developers to build entire applications with it.

Reference:

History of Javascript:

  • JavaScript was created in May 1995 by Brendan Eich, a developer at Netscape Communications Corporation.
  • It was initially developed in just 10 days and was first named Mocha, later renamed LiveScript, and finally became JavaScript.
  • It was first implemented in Netscape Navigator 2.0, one of the most popular web browsers of the time.
  • In 1997, JavaScript was standardized as ECMAScript by the European Computer Manufacturers Association (ECMA) to ensure compatibility across different browsers.
  • The first version of ECMAScript, ES1, was released in June 1997.

History of JS

Reference:

History of JavaScript - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

favicon geeksforgeeks.org

How Js Works?

  • JavaScript is a single-threaded, event-driven language that runs in environments like browsers and Node.js.
  • It executes code using a JavaScript engine (e.g., V8, SpiderMonkey) and manages asynchronous operations via the event loop.

VARIABLES IN JAVASCRIPT:

  • In JavaScript, a variable is a container for storing data values.
  • Variables are fundamental to programming as they allow you to store, manipulate, and retrieve data.

Rules for Naming Variables:

  • Must start with a letter, underscore (_), or dollar sign ($).
  • Can contain letters, numbers, underscores, or dollar signs.
  • Case-sensitive (age and Age are different).
  • Cannot be a JavaScript reserved keyword (e.g., let, class, return).

Declaring Variables:
1.Using var:

  • The var keyword was traditionally used to declare variables in JavaScript.
  • Variables declared with var are function-scoped and can be re-declared within the same scope without causing an error.

Scope:
Function-scoped (or globally scoped if declared outside a function).
Hoisting:
Hoisted to the top of the scope and initialized with undefined.
Redeclaration:
Allowed within the same scope.

syntax:
// var
var name = "Alice";

example:
var x = 10;
var x = 20; // Redeclaration allowed
console.log(x); // 20

2.Using let:

  • The let keyword was introduced in ES6 (2015) and is block-scoped.
  • This means that a variable declared with let is only accessible within the block it was declared in.
  • Unlike var, let does not allow re-declaration within the same scope.

Scope:

  • Block-scoped ({ ... }). Hoisting:
    • Hoisted but not initialized (Temporal Dead Zone until declared). Redeclaration:
    • Not allowed in the same scope.

syntax:
// let
let age = 25;

example:
let y = 10;
// let y = 20; // ❌ Error: Cannot redeclare
y = 20; // ✅ Allowed
console.log(y); // 20

3.Using const:

  • The const keyword is also block-scoped and is used to declare variables that should not be reassigned.
  • However, the contents of objects and arrays declared with const can still be modified.

Scope:

  • Block-scoped. Hoisting:
  • Hoisted but not initialized (Temporal Dead Zone). Redeclaration:
  • Not allowed. Reassignment:
  • Not allowed (value is constant).

syntax:
// const
const country = "India";

example:
const z = 10;
// z = 20; // ❌ Error: Assignment to constant variable
console.log(z); // 10

Special Variable Types:

Global Variables:
Declared outside any function/block; accessible everywhere.
Local Variables:
Declared inside a function/block; accessible only there.
Static Variables:
Retain value between calls.(to be discussed)

Reference:

Top comments (0)