Outdoor Adventure
  • Home
  • About
  • Contact
No Result
View All Result
Outdoor Adventure
  • Home
  • About
  • Contact
No Result
View All Result
Outdoor Adventure

JavaScript Variables – Decode Now

admin by admin
March 11, 2024
in Javascript
A A
JavaScript Variables
Share on FacebookShare on Twitter

Last Updated on March 11, 2024 by

JavaScript Variables

  • In JavaScript, a variable is a container used to store data values.
  • Variables are declared using keywords such as “var”, “let”, or “const”, followed by the variable name.
  • The value of the variable can be assigned using the assignment operator “=”.
  • For example, the following code declares a variable `x` and assigns a value `5` to it: var x = 5;
  • Variables in JavaScript can hold different types of data, such as numbers, text strings, booleans, arrays, and objects. The type of the variable can change dynamically during the runtime of the program.
  • JavaScript variables are used to store and manipulate information in a program.

JavaScript Keywords :-

 

– Var Keyword

  • Variables declared using the var keyword are either globally or functionally scoped, and do not support block-level scope, if a variable is defined in a loop or an if statement, it can be accessed outside the block and accidentally redefined leading to a buggy program. On the other hand, let and const keywords are block-scoped, meaning a variable declared using let or const is only accessible within the block it was declared.
  • Variable names are case-sensitive in JavaScript, meaning variable names like msg, MSG, Msg, and mSg are considered separate variables. Variable names can contain letters, digits or the symbols $ and _. A variable name cannot start with a digit 0-9. A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, and return cannot be variable names [Source 2].
  • To declare a variable, one can use the var keyword followed by the variable name and the value assigned to it, as shown below:  var age = 25;
  • The var keyword was JavaScript’s first way to declare a variable, but it has some baggage.
  • JavaScript variables were always defined at the function scope, which meant that variables were global. JavaScript didn’t enforce block-level scopes such as inside a for loop or if block. So a variable declared with var would be hoisted to the top of its function scope.

– Let Keyword

  • The `let` keyword in JavaScript is used to declare block-scoped variables. This means that variables declared with `let` are only accessible within the block they are declared in (including any nested blocks), and are not accessible outside of that block.
  • Let us see the syntax for this,  let name1 [= val1] [, name2 [= val2]] [, …, namen [= valn];
  • JavaScript only had function-scoped variables declared using the `var` keyword, which meant that variables declared with `var` were accessible throughout the entire function, regardless of where they were declared. With `let`, developers can now declare variables with a more fine-grained scope, making their code easier to reason about.

Here is an example of using `let` to declare a variable within a block:

Related Post

JavaScript Operators

JavaScript Operators – Decode Now

March 11, 2024
Javascript and its Features

Javascript and its Features

March 4, 2024

function foo() {
if (true) {
let x = 10;
console.log(x); // output: 10
}
console.log(x); // output: Uncaught ReferenceError: x is not defined
}

In this example, the variable `x` is only accessible within the `if` block and cannot be accessed outside of it.

– Const Keyword

  • The `const` keyword in JavaScript is used to declare a variable that cannot be reassigned after its initial assignment. Once a variable is declared with the `const` keyword, its value cannot be changed.
  •  It is a block-scoped variable, meaning that its scope is limited to the block in which it is declared.

Here are some key features of the `const` keyword in JavaScript:

  •  A `const` variable must be assigned a value when it is declared. If it is not assigned a value, it will result in a `SyntaxError`.
  •  Once a `const` variable is assigned a value, its value cannot be changed. Attempting to reassign a `const` variable will result in a `TypeError`.
  •  A `const` variable is block-scoped, meaning that its scope is limited to the block in which it is declared.
  •  A `const` variable can be used to declare an array or object, but its properties or elements can still be modified.
  •  The `const` keyword is used to declare a variable that is intended to have a constant value throughout the program.

Here is an example of declaring a `const` variable:

const PI = 3.14;

  • In this example, `PI` is assigned the value of `3.14`, and cannot be reassigned to a different value later in the program.

Here is an example of  declaring a `const` variable with an object:

       const person = {
name: “John”,
age: 30,
};

  • In this example, `person` is assigned an object with two properties: `name` and `age`. Although the `person` variable cannot be reassigned to a different object, the properties of the `person` object can still be modified.

Note :-

  • If you want to learn Javascript, Above are the key points which will help you to grow more in your career.
  • Also, if you like this post or have any queries, Do let me know in the comments.
  •  Below are the resources to learn more about Javascript.

Resources :-

  • JavaTpoint
  • Geeks for Geeks
  • W3School
  • Javascript.info
  • Programiz
  • MDN Documentation
Tags: javascriptjs
admin

admin

Related Posts

JavaScript Operators
Javascript

JavaScript Operators – Decode Now

by admin
March 11, 2024
Javascript and its Features
Javascript

Javascript and its Features

by admin
March 4, 2024
Next Post
JavaScript Operators

JavaScript Operators - Decode Now

JavaScript Type Conversion

JavaScript Type Conversion - Decode Now

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
JavaScript Variables

JavaScript Variables – Decode Now

March 11, 2024
Javascript Roadmap

Best Javascript Roadmap 2024

September 15, 2024
Understanding JavaScript Closures

Understanding JavaScript Closure

August 1, 2024
JavaScript Object methods

Introducing JavaScript Object Methods

June 1, 2024
HTML Roadmap

Best HTML Roadmap For Programmers

12
Javascript and its Features

Javascript and its Features

4
Mastering JavaScript Error Handling

Mastering JavaScript Error Handling

1
CSS Roadmap

Best CSS Roadmap 2024

0
Mastering JavaScript Error Handling

Mastering JavaScript Error Handling

August 10, 2024
Understanding JavaScript Closures

Understanding JavaScript Closure

August 1, 2024
Javascript Hoisting

Understanding JavaScript Hoisting

July 16, 2024
JavaScript Object methods

Introducing JavaScript Object Methods

June 1, 2024

About

The best Premium WordPress Themes that perfect for news, magazine, personal blog, etc.

Categories

  • Javascript
  • Roadmaps
  • Uncategorized

Tags

css html java javascript JavaScript Loops js roadmap

Recent Post

  • Mastering JavaScript Error Handling
  • Understanding JavaScript Closure
  • Purchase Now
  • Features
  • Demo
  • Support

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Landing Page
  • Buy JNews
  • Support Forum
  • Pre-sale Question
  • Contact Us

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.