Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for JS Variables
Armen Ghazaryan
Armen Ghazaryan

Posted on

     

JS Variables

JavaScript is mostly used on:

  • Web development.
  • Web applications.
  • Web servers.
  • Mobile applications.
  • Games development.

etc.

It's the most used programming language in the world, used as a client-side programming language by 97.0% of all websites.

As in other programing languages, in JavaScript there are different data types:

  • Strings.
  • Arrays.
  • Objects.
  • Functions.

Declaring a variable is a way of storing data. It can be declared by keywordsvar,let andconst. Thevar keyword is used in all JavaScript code from 1995 to 2015. Thelet andconst keywords were added to JavaScript in 2015. If there is a need of running code in an old browser, variables should be declared withvar.
There are some restrictions that we need to follow at a time of getting name to the variable. We cannot use spaces or tabs, and the name cannot start with the number. Names of variables are case sensitive: words with uppercase and lowercase letters are different.

Variables can contain different types of information: Numbers, Strings, Booleans, etc.

String is used with double or single quotes and contains a group of characters.
Boolean data type has two values: true and false.

Here is how variables are declared:

var name = "Armen";let AUAstudent = true;const age = 18
Enter fullscreen modeExit fullscreen mode

When we use theconst for declaring variable, then the variable cannot be changed or reassigned.

We can change the value of the variables declared withlet orvar. For example:
let course = “Introduction to Computer Science”;
for changing the variable course we write:
course = “Linear Algebra”;
In this way we assign a new value to the variable.

We can also do mathematical operations inside the variables, such
as
let a = 5+6;

The main difference betweenvar andlet is the scope. Via scopes we can know where the variable can be used.
There are two type of scopes:

  1. global
  2. local

Global variables can be redeclared outside of a block, and they are accessible from every point of the code. Itsvar.

Local variables are declared inside of a block. Itslet. Let variables need to be declared before use.

Let allows us to declare variables which are limited to a block statement butvar lets us redeclare the variables.

var x = 17
Enter fullscreen modeExit fullscreen mode

x is global variable here.

function() {let n = 11alert(n)}
Enter fullscreen modeExit fullscreen mode

n is local variable here.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromArmen Ghazaryan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp