Be Sociable, Share!

sábado, 14 de mayo de 2022

Review Variables

 

  1. Variables hold reusable data in a program and associate it with a name.
  2. Variables are stored in memory.
  3. The var keyword is used in pre-ES6 versions of JS.
  4. 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.
  5. Variables that have not been initialized store the primitive data type undefined.
  6. Mathematical assignment operators make it easy to calculate a new value and assign it to the same variable.
  7. The + operator is used to concatenate strings including string values held in variables.
  8. In ES6, template literals use backticks ` and ${} to interpolate values into a string.
  9. The typeof keyword returns the data type (as a string) of a value.

jueves, 12 de mayo de 2022

Mathematical Assignment Operators

 let w = 4;

w = w + 1;

console.log(w); // Output: 5


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






let levelUp = 10;

let powerLevel = 9001;

let multiplyMe = 32;

let quarterMe = 1152;


// Use the mathematical assignments in the space below

// These console.log() statements below will help you check the values of the variables.

// You do not need to edit these statements. 

console.log('The value of levelUp:', levelUp); 

console.log('The value of powerLevel:', powerLevel); 

console.log('The value of multiplyMe:', multiplyMe); 

console.log('The value of quarterMe:', quarterMe);

levelUp+=5;

powerLevel-=100;

multiplyMe*=11;

quarterMe/=4;


Just like the previous mathematical assignment operators (+=-=*=/=), the variable’s value is updated and assigned as the new value of that variable. 


let gainedDollar = 3;

let lostDollar = 50;

gainedDollar++;

lostDollar--;


let myName = 'Natalia';

let myCity = 'Mexico City';

console.log(`My name is ${myName}. My favorite city is ${myCity}.`)


lunes, 9 de mayo de 2022

JAVASCRIPT Built-in Objects: Prints

 

When we use console.log() we’re calling the .log() method on the console object. 

Let’s see console.log() and some real string methods in action!


  • Data is printed, or logged, to the console, a panel that displays messages, with console.log().

  • We can write single-line comments with // and multi-line comments between /* and */.

  • There are 7 fundamental data types in JavaScript: strings, numbers, booleans, null, undefined, symbol, and object.

  • Numbers are any number without quotes: 23.8879

  • Strings are characters wrapped in single or double quotes: 'Sample String'

  • The built-in arithmetic operators include +-*/, and %.

  • Objects, including instances of data types, can have properties, stored information. The properties are denoted with a . after the name of the object, for example: 'Hello'.length.

  • Objects, including instances of data types, can have methods which perform actions. Methods are called by appending the object or instance with a period, the method name, and parentheses. For example: 'hello'.toUpperCase().

  • We can access properties and methods by using the ., dot operator.

  • Built-in objects, including Math, are collections of methods and properties that JavaScript provides.

//random number prints

console.log(Math.random() *100);

// * 100

console.log(Math.floor(Math.random() *100));

//ceil number, smaller than 43.8

console.log(Math.ceil(43.8));

// Integer number, false or true

console.log(Number.isInteger(2017));

In JavaScript, there are seven fundamental data types

 

  • Number:  Any number, including numbers with decimals: 48151623.42.
  • String: Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes: ' ... ' or double quotes " ... ", though we prefer single quotes. Some people like to think of string as a fancy word for text.
  • Boolean: This data type only has two possible values— either true or false (without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
  • Null: This data type represents the intentional absence of a value, and is represented by the keyword null (without quotes).
  • Undefined: This data type is denoted by the keyword undefined (without quotes). It also represents the absence of a value though it has a different use than nullundefined means that a given value does not exist.
  • Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.
  • Object: Collections of related data.