variables en javascript code example

Example 1: javascript declare a variable

//choose the best for your solution
var myVariable = 22; //this can be a string or number. var is globally defined

let myVariable = 22; //this can be a string or number. let is block scoped

const myVariable = 22; //this can be a string or number. const is block scoped and can't be reassigned

Example 2: js variables

//Making variables with let:
let numberOfFriends = 1;

//Incrementing:
numberOfFriends += 3; //numberOfFriends is now 4

// Variables with const
const minimumAge = 21; //CANNOT REASSIGN!

//Booleans - true or false values
true;
false;
let isHappy = true;

//Naming Conventions
// Use upper camel-cased names:
let numberOfChickens = 6; //GOOD
// NOT THE JS WAY:
// let number_of_chickens = 6;

Example 3: javascript define variable

var variable1 = "some variable content";

Example 4: how to declare variables javascript

//variables are a way to easily store data in javascript
//variables are declared by the var keyword, example...
var x = 10;
//or
var dog_name = "daisy";
//which can also be written as 
var dog_name = 'daisy';
//same thing with single quotes and double quotes

Example 5: javascript inicializar una variable

var a = 1;
b = 4;

delete this.a; // Lanza un error de tipo "ReferenceError" en modo estricto ('use strict'), de lo contrario falla silenciosamente.
delete this.b;

console.log(a, b); // Lanza un error de tipo "ReferenceError". 
// La propiedad 'b' se eliminó y ya no existe.

Tags:

Html Example