javaScript D-3

Create a Variable: var


  • Variable names cannot start with numbers.
  • Variable names are case sensitive
  • myName is the variable’s name. Capitalizing in this way is a standard convention in JavaScript called camel casing.
  • = is the assignment operator. It assigns the value ('Arya') to the variable (myName).
  • 'Arya' is the value assigned (=) to the variable myName. You can also say that the myName variable is initialized with a value of 'Arya'.
  • After the variable is declared, the string value 'Arya' is printed to the console by referencing the variable name: console.log(myName).
var favoriteFood  = 'pizza';
console.log(favoriteFood );
var numOfSlices = 8;
console.log(numOfSlices);
var numberOfSlices = favoriteFood;
comsole.log(numofSlices);

Create a Variable: let


 The let keyword signals that the variable can be reassigned a different value.

let meal = 'Enchiladas'; console.log(meal); // Output: Enchiladas meal = 'Burrito'; console.log(meal); // Output: Burrito

Another concept that we should be aware of when using let (and even var) is that we can declare a variable without assigning the variable a value. In such a case, the variable will be automatically initialized with a value of undefined:

let price; console.log(price); // Output: undefined price = 350; console.log(price);

let changeMe = true;
console.log(changeMe);
changeMe = false;
console.log(changeMe);

Create a Variable: const


Just like with var and let you can store any value in a const variable. The way you declare a const variable and assign a value to it follows the same structure as let and var

const myName = 'Gilberto'; console.log(myName);


However, a const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you’ll get a TypeError.

Mathematical Assignment Operators


let w = 4; w = w + 1; console.log(w);


Another way we could have reassigned w after performing some mathematical operation on it is to use built-in mathematical assignment operators. We could re-write the code above to be:

let w = 4; w += 1;
console.log(w); // Output: 5
We also have access to other mathematical assignment operators: -=*=, and /= which work in a similar fashion.

let x = 20; x -= 5; // Can be written as x = x - 5 console.log(x); // Output: 15 let y = 50; y *= 2; // Can be written as y = y * 2 console.log(y); // Output: 100 let z = 8; z /= 2; // Can be written as z = z / 2 console.log(z); // Output: 4

The Increment and Decrement Operator

let a = 10; a++; console.log(a);

let b = 20; b--; console.log(b);

Other mathematical assignment operators include the increment operator (++) and decrement operator (--).
The increment operator will increase the value of the variable by 1. The decrement operator will decrease the value of the variable by 1. For example:
let gainedDollar = 3;
let lostDollar = 50;
gainedDollar++;
lostDollar--;

String Concatenation with Variables


The + operator can be used to combine two string values even if those values are being stored in variables:

let myPet = 'armadillo'; console.log('I own a pet ' + myPet + '.'); // Output: 'I own a pet armadillo.'

String Interpolation


we can insert, or interpolate, variables into strings using template literals. Check out the following example where a template literal is used to log strings together:

const myPet = 'armadillo'; console.log(`I own a pet ${myPet}.`);
a template literal is wrapped by backticks ` (this key is usually located on the top of your keyboard, left of the 1 key).

Inside the template literal, you’ll see a placeholder, ${myPet}. The value of myPet is inserted into the template literal.

When we interpolate `I own a pet ${myPet}.`, the output we print is the string: 'I own a pet armadillo.'

One of the biggest benefits to using template literals is the readability of the code. Using template literals, you can more easily tell what the new string will be. You also don’t have to worry about escaping double quotes or single quotes.


const myName = 'biplav';
const myCity = 'banglore';
console.log(`My name is ${myName}. My favorite city is ${myCity}.`);

typeof operator


If you need to check the data type of a variable’s value, you can use the typeof operator.


The typeof operator checks the value to its right and returns, or passes back, a string of the data type.

const unknown1 = 'foo'; console.log(typeof unknown1); // Output: string const unknown2 = 10; console.log(typeof unknown2); // Output: number const unknown3 = true; console.log(typeof unknown3); // Output: boolean

Let’s break down the first example. Since the value unknown1 is 'foo', a string, typeof unknown1 will return 'string'.

let newVariable = 'Playing around with typeof.';
console.log(typeof newVariable);
newVariable = 1;
console.log(typeof newVariable);
console.log(typeof newVariable);
VARIABLES

Review Variables

  • Variables hold reusable data in a program and associate it with a name.
  • Variables are stored in memory.
  • The var keyword is used in pre-ES6 versions of JS.
  • let is the preferred way to declare a variable when it can be reassigned, and const is the preferred way to declare a variable with a constant value.
  • Variables that have not been initialized store the primitive data type undefined.
  • Mathematical assignment operators make it easy to calculate a new value and assign it to the same variable.
  • The + operator is used to concatenate strings including string values held in variables
  • In ES6, template literals use backticks ` and ${} to interpolate values into a string.
  • The typeof keyword returns the data type (as a string) of a value.
Instructions
To learn more about variables take on these challenges!
  • Create variables and manipulate the values
  • Check what happens when you try concatenating strings using variables of different data types
  • Interpolate multiple variables into a string
  • See what happens when you use console.log() on variables declared by different keywords (constletvar) before they’re defined. For example:
  • Find the data type of a variable’s value using the typeof keyword on a variable.
  • Use typeof to find the data type of the resulting value when you concatenate variables containing two different data types.









Comments

Popular Posts