
Variables are values that can change based on conditions or information passed to the program. Actually, variables do programs function as a set of instructions that instruct a computer what to do. Most programs use data as they run. You can represent data as either constant (fixed values that don't change) or as variables (variable values).
Data Types
An important concept in programming is the data type. Data types specify the types of values a variable can hold and the operations it can perform. JavaScript presents a very flexible data type system. It is referred to as "loosely typed" or "weakly typed". As a result, even though the variable may start out as one type, it can be changed into another type later on. "Strongly" typed languages cannot change the type of variables once they have been assigned to it. "Strongly" typed languages provide more type safety, meaning you can assume that a variable will always be a certain type in a "strongly" typed language. However, weakly typed languages, on the other hand, are more flexible.
There are some common data types in even "weakly" typed languages. In most languages, the following data types exist:
jsnull // a defined non-valueinteger //a numberboolean //a true or false valuecharacter //a single characterfloating-point number //a number with a decimal pointstring // a group of characters (i.e. a word or multiple words)
Another JavaScript type is undefined, which indicates that a variable is not defined. The difference between null and undefined is that null is a value that is defined and set to null, while undefined is a variable without a definition. Collectively, these types of data are known as "primitive" types in JavaScript. JavaScript provides the following kinds of "complex" data types:
Array: A high-level, list-like object contains values, as well as alength
jslet myArr = [1, 2, 3]
Object: An object of JavaScript has properties when its root level is defined as an object
jslet myObj = { foo: 1, bar: 2 }
Function: A type of object that performs a computation when invoked. Many arguments are often passed in, and for some, they return values as well
js// Function declarationfunction multiply(arg1, arg2) {return arg1 * arg2}// Function usagelet result = multiply(2, 4)
Defining Variables In JavaScript
Most programs require the definition of variables. As in other languages, JavaScript has the requirement of defining variables before they can be used. Typically, the flow of a file in a program is top to bottom, so normally the variable definition appears before the variable usage. Take a look at the following code:
js// Variable definitionvar num = 2// Variable usageconsole.log(num)
In the code above the variable num is already defined, and it is assigned a value of 2 (an integer). Afterward, it is passed as an argument to a "function" called console.log. Because JavaScript is a weakly typed language, you do not need to explicitly declare num as an integer. In other strongly typed languages, such as C# (another common language) you might see numeric types explicitly defined, such as:
js// Variable definitionint num = 2;// Variable usageConsole.Write(num);
As in the JavaScript code above, the C# code would accomplish the same task. According to the JavaScript code above, the var keyword is used to define the variable num. In modern JavaScript programs, people do not usually use this keyword. There are three different keywords you can use:
var: Variables can be declared, and their value can be initialized
jsvar x = 1if (x === 1) {var x = 2// expected output: 2console.log(x)}// expected output: 2console.log(x)
let: An optional value is assigned to a block-scoped variable
jslet x = 1if (x === 1) {let x = 2// expected output: 2console.log(x)}// expected output: 1console.log(x)
const: This declares a local variable that is block-scoped. Values are constants and cannot be changed or redeclared
jsconst number = 42try {number = 99} catch (err) {// expected output: TypeError: invalid assignment to const `number'// Note - error messages will vary depending on browserconsole.log(err)}// expected output: 42console.log(number)
maximum programmer used const and let rather than var for safety reasons. If you notice in the code above for let and var, let variables share almost all of the characteristics of var which includes one more function. There is a concept known as the "block scope" of a let variable. In a moment, we will discuss what that means, but notice above that in the let block x is re-assigned to "2" inside an "if block," but remains as "1" outside the block.
Naming Conventions
There are naming conventions in JavaScript, which specify how variables should be named. In general, you should describe what your variable represents
js// badlet str = 2// goodlet num = 2
Additionally to naming variables descriptively, different types of variables are named with lowercase and uppercase letters. In general, JavaScript uses what's called "camelCase" with most variables. In "camelCase" the first letter of any variable name is lowercase, and all subsequent words in the variable begin with uppercase letters. There are some data types in JavaScript that should be "CapitalCase," and you will learn about them later. The most common constant used by people is "UPPER_SNAKE_CASE."
all posts