Go Back   Site Owners Forums - Webmaster Forums > Web Programming > Programming General

Notices


Reply
 
Thread Tools Rate Thread Display Modes
Old 03-03-2023, 08:23 AM   #1
sahithya
Registered User
 
Join Date: Feb 2013
Location: Bangalore
Posts: 1,095
Var vs Let vs Const

Var
Variables declared with *var* are function scoped, meaning they are accessible within the function they are declared in. If a *var* variable is declared outside of a function, it becomes a global variable and is accessible throughout the entire program. *var* variables can be redeclared and updated within their scope.
It is generally recommended to avoid using *var*, as its behavior with respect to scope can be confusing. However, it may still be used in legacy code, or in cases where you need a variable with function scope that needs to be updated within the function.

Let
Variables declared with *let* are block scoped, meaning they are only accessible within the block they are declared in. This makes them more suitable for use in loops and conditional statements. Like *var*, *let* variables can be updated within their scope, but they cannot be redeclared.
Use *let* when you want to declare a variable that will be re-assigned later, or when you need a block-scoped variable (for example, in a loop).

Const
Variables declared with *const* are also block scoped, but they cannot be updated or redeclared after their initial assignment. This makes them useful for declaring constants or values that should not change.
It is recommended to use *const* whenever you want to declare a constant value that should not be re-assigned.

In general, prefer *const* over *let* and avoid using *var* when possible.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
sahithya is offline   Reply With Quote

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:36 PM.


Powered by vBulletin Copyright © 2020 vBulletin Solutions, Inc.