Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8216

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T11:04:07+00:00 2024-11-28T11:04:07+00:00

Beginners guide to JavaScript Variables

  • 60k

Hello World, JavaScript is one of the most popular web technologies that when mastered will open a new door of creativity and power in building web applications.
With the JavaScript language, we deal with a lot of data or information, this data or information needs to be stored somewhere we can quickly have access to.
The box or container for storing data are variables.
In this post, we take a look at variables in JavaScript, we will learn what variables are, how to create variables, how to store values and how to access the stored values.

Let's get started

What are variables?

With most web applications, you may want to work with some information or data. For instance, if you plan to build the next social media application, you may want to keep information like who created an account, who posted, who liked a post etc.
The way to keep track of these informations or data is store them in variables.

Variables are boxes that stores values or information or data.

The value or data stored can then be used during the program execution.
Every variable has three main structures

  • The name : to uniquely identify the variable
  • The value : referring to the data or information stored in the variable
  • The memory address : referring to the location in the computer's memory where the variable is stored.

Let's learn how to create variables.

Declaring a variable
To be able to store data in a variable, you need to declare it.

  • You declare a variable with the var (less recommended) or let or const keywords.
  • Followed by the *name * you want to give to the variable.

For instance

var firstName; 
Enter fullscreen mode Exit fullscreen mode

or

let firstName; 
Enter fullscreen mode Exit fullscreen mode

From the above, I have created a 'container' or box with a label **firstName.
After declaring the variable, you can now **store the data or value into the variable. The syntax will be
var variableName = value;
Let's now assign the firstName variable a value

let firstName = "Vivian"; 
Enter fullscreen mode Exit fullscreen mode

We have now stored the data or value “Vivian” in our firstName variable (or container).
You can also declare multiple varialbes and assign values to each of them.

let firstName = "Vivian"; let lastName = "Sylvanus"; let email = "vivaluv98@gmail.com" 
Enter fullscreen mode Exit fullscreen mode

The above declare three variables firstName, lastName, and email and store three distinct piece of data or value in them.
We can also declare multiple variables at a go, each variable declaration must be followed by a , (comma).

//declaring 3 variables together let firstName = "Vivian", lastName ="Sylvanus", email="vivaluv98@gmail.com"; 
Enter fullscreen mode Exit fullscreen mode

Rules for variable name
The following are some rules to take note of when declaring variables

  • Variable names cannot contain spaces
  • The first letter of the variable can be [a-z, A-z], dollar sign ($), or underscore(_)
  • Any digit can be used after the first letter
  • Variable names are case sensitive. For instance let firstName and let FirstName are not the same

Undefined vrs Undeclared Variables
An undefined variable is a variable that has been declared, but has not been assigned a value. Because it has not been assigned a value, the variable uses undefined as its initial value.
Let's declare a variable not assign it a value and see what the output will be

let firstName; console.log(firstName) 
Enter fullscreen mode Exit fullscreen mode

The output will be undefined
However, an undeclared variable is a variable that has not been declared. Accessing an undeclared variable will produce a ReferenceError.
For instance

console.log(message); //ReferenceError: message has not been declared.  
Enter fullscreen mode Exit fullscreen mode

Retrieving Values
To easily grasp the concept of variable, you can imagine it as box with a unique name or label on it, used for storing data. We can put any value or data in the box.
To access the data or value in the box, you need to call the variable name (type the unique name you gave to the variable).

let firstName = "Vivian" //retrieving the value firstName; 
Enter fullscreen mode Exit fullscreen mode

To dispay the output of a variable, you can use the console.log() method and insert the variable name in the () parenthesis.
So, if you want to view what data is stored in the firstName variable , you can write

console.log(firstName); /*This will output the data stored in firstName in the developer console.*/ 
Enter fullscreen mode Exit fullscreen mode

You can also change the value or data stored in the variable by assigning the variable a new data.
See the code below:

firstName = "Chiamaka"; //change the value stored in the variable to Chiamaka  console.log(firstName); //Chiamaka  
Enter fullscreen mode Exit fullscreen mode

Now when you call the firstName variable, it will contain the value Chiamaka instead of Vivian
Declaring a variable twice
A variable should be declared only once, a repeated declaration of the same variable is an error

let firstName = "Vivian"; let firstName = "Chiamaka "; /*SyntaxError: 'firstName' has already been declared */ 
Enter fullscreen mode Exit fullscreen mode

Types of variables
All variables exist within a scope, which determines which part of the code can have access to the variables, and the life span of the variables.
There are two types of variables supported in JavaScript

  • Local variables
  • Global variables

Local Variables

A local variable is a variable that is declared inside a code block, a function body or inside a loop body.

  • In other words, if we declare a variable inside a curly braces {} or block scope, it is a local variable. That variable can only be accessed inside that scope or {}
  • Also, if you declare a variable inside a function, JavaScript will add the variable to the function's scope, the variable will only exist inside the function
  • It is recommended to use let keyword when declaring local variables.

Let's examine the code below:

function someFunc(){     let firstName ="Vivian"; //accessing the local varialbe     console.log(firstName) } someFunc() // output "Vivian"  //accessing the variable outside the scope {} console.log('access out scope', firstName) // Uncaught ReferenceError: firstName is not defined 
Enter fullscreen mode Exit fullscreen mode

  • Since firstName variable is declared inside the curly braces {} or a function scope, it is a local variable and cannot be accessed outside {}.
  • If you try to access firstName outside of the function, as in the example above, you will get a ReferenceError because the firstName variable was not defined.

Global variable

A variable declared anywhere within the script and not declared inside a block scope or function scope is a global variable

In simple words, if the variable was not declared inside a function's body or in a block of code {}, then it s a global variable. **
Global variables can be accessed **anywhere in your code.
It is recommended to use the var keyword for global variables.
Naming things right
It is recommended that the name you give the variable, should have an obvious meaning, describing the data that it stores.
A quick glance at variable names can reveal if the program was written by a novice or an experienced developer.
Some good to follow rules are

  • Use identifiable and human-readable names like userName , firstName or tasks
  • Avoid using abbreviations or short names like a, , usr etc. Conclusion

In summary, you have learnt that:

  • Variables are like boxes for keeping data or information
  • We can declare variables using var, let and const keywords.
  • To access the value of the variable, call the variable name
  • Lastly, variables should be named in a way that will help easily understand what is inside them.

If you have found value, in this post, kindly leave a comment. Help other #codenewbies by sharing the post.
Written with love from Lagos Nigeria,( thank you )

codenewbiejavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.