Skip to main content

Command Palette

Search for a command to run...

JavaScript Variables

Published
•2 min read•View as Markdown
JavaScript Variables
L

I am a proactive Full-Stack Software Engineer with a background in Business and Maritime Management. I specialized in JavaScript, HTML, CSS, NodeJS, ExpressJS, ReactJS, NextJS, TypeScript and more. I possess strong skills in problem solving, communication, and collaboration that leads to quality products & results. I also love Meeting with the software development team to define the scope and scale of software projects thereby leading to the software architectural design. I am equally eager to expand my knowledge and implement what I have learnt in a practical and professional environment.

In JavaScript, a variable is a named storage location that holds a value. Variables are used to store and manipulate data in a program. They are declared using the var, let, or const keywords.

Here are some examples of how to use variables in JavaScript:

In this example, we declare three variables x, y, and z using the var, let, and const keywords, respectively. We assign values to these variables and then use the console.log() function to print their values to the console.

Note that variables declared using the var keyword are function-scoped, while variables declared using let and const are block-scoped. This means that variables declared using var are accessible anywhere within the function in which they are declared, while variables declared using let and const are only accessible within the block in which they are declared.

Here is an example that demonstrates the difference between function-scoped and block-scoped variables:

In this example, we declare three variables a, b, and c within the myFunction function. We then declare another set of variables with the same names within an if block.

Note that the var keyword is used for a both inside and outside of the block, while let and const are used for b and c, respectively.

When we print the values of a, b, and c inside the if block, we get the values 4, 5, and 6, respectively. When we print the values of a, b, and c outside the if block, we get the values 4, 2, and 3, respectively. This demonstrates the difference between function-scoped and block-scoped variables in JavaScript.

Overall, variables are an essential concept in JavaScript that allow for storing and manipulating data in a program. By understanding how to declare and use variables in JavaScript, you can write more effective and efficient code.

That's it for JavaScript Variables. Thanks ❤️

More from this blog

Luke Chidubem's blog

29 posts