What are Variables in JavaScript

Variables are a foundational aspect of programming with JavaScript, or any programming Language.

A variable must have a unique name.

WHAT ARE VARIABLES?

You use variables as symbolic names for values in your application.

The names of variables, called identifiers, conform to certain rules.

RULES

An identifier must start with a letter, underscore (__), or a dollar sign ($). Subsequent characters can

also be digits (0-9).

Letters include the characters “A” through “Z”

(uppercase) as well as “a” through “z” (lowercase).

Unicode letters such as å and ü can be used in identifiers. Unicode escape sequences as

characters can also be used in identifiers.

DECLARING VARIABLES

var x = 42

let y = 13

This syntax can be used to declare local and global variables.

let { bar }= foo

This will create a variable named bar and assign to it the value corresponding to the key of the same name from our object foo.

EVALUATING VARIABLES

A variable declared using the var or let statement with no assigned value specified has the value of undefined.

An attempt to access undeclared variable results in a Reference Error exception being thrown:

SYNTAX

JavaScript borrows most of its syntax from Java, C, and C++, but it has also been influenced by A wk, Perl, and Python.

FEATURES

JavaScript is case-sensitive.

JavaScript uses the Unicode character set.

In JavaScript, instructions are called statements and are separated by semicolons (;). It is considered best practice, to always write a semicolon after a statement, it reduces the chances of bugs getting into the code.

COMMENTS

// a one-line comment

/* this is a longer,

*multi-line comment

*/

/* You can’t, however, /* nest

Comments */ Syntax Error */

DECLARATIONS

var

Declares a variable, optionally initializing it to a value.

let

Declares a block-scoped, local variable, optionally initializing it to a value.

Const

Declares a block-scoped, read-only named constant.

Leave a Reply

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