var number = 5; // in-line comment
/*
Multi-line comment 1
Multi-line comment 2
Multi-line comment 3
*/
// try to use semi-colon
var a; // here we are declaring a variable
var b = 2; // here we are declaring and assinging in one line
// equal (=) sign is an assinging operator
// Initialize these three variables
var a = 5;
var b = 10;
var c = "I am a";
// updating above variables
a = a + 1; // 6
b = b + 5; // 15
c = c + " String!"; // "I am a String!"
// Declarations
var StUdLyCapVaR;
var properCamelCase;
var TitleCaseOver;
// Assignments
STUDLYCAPVAR = 10;
PRoperCAmelCAse = "A String";
tITLEcASEoVER = 9000;
// this will give error
In above code the declared variable are not same as variable used during assignment.
// Declarations with camel case
var studlyCapVar;
var properCamelCase;
var TitleCaseOver;
// Assignments
studlyCapVar = 10;
properCamelCase = "A String";
TitleCaseOver = 9000;
var sum = 10 + 0; // 10
var difference = 45 - 3 // 42
var product = 8 * 10; // 80
var quotient = 66 / 33; // 2
var myVar = 87;
myVar = myVar + 1;
myVar++; // shortcut
var myVar = 11;
myVar = myVar - 1;
myVar--;
var ourDecimal = 5.7;
var product = 2.0 * 2.5;
var quotient = 4.4 / 2.0;
var remainder = 11 % 3;
var a = 3;
var b = 17;
var c = 12;
a += 12; // same as a = a + 12
b += 9;
c += 7;
var a = 11;
var b = 9;
var c = 3;
a -= 6;
b -= 15;
c -= 1;
var a = 5;
var b = 12;
var c = 4.6;
a *= 5;
b *= 3;
c *= 10;
var a = 48;
var b = 108;
var c = 33;
a /= 12;
b /= 4;
c /= 11;
var firstName = "Alan";
var lastName = 'Turing';
// var myStr = "I am a "double quoted" string inside "double quotes"";
// by using "\" we can escape quotes
var myStr = "I am a \"double quoted\" string inside \"double quotes\"";
// var myStr = "<a href=\"http://www.example.com\" target =\"_blank\">Link<a/>";
// use single quote in beginning and in ending.
var myStr = '<a href="http://www.example.com" target ="_blank">Link<a/>';
var myStr = `'<a href="http://www.example.com" target ="_blank">Link<a/>'`;
CODE OUTPUT
\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed
var ourStr = "I come first. " + "I come second.";
var ourStr = "I come first. ";
ourStr += "I come second."
console.log(ourStr); // "I come first. I come second."
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
console.log(ourStr); // "Hello, our name is freeCodeCamp , how are you?"
var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is ";
ourStr += anAdjective;
console.log(ourStr); // "freeCodeCamp is awesome!"
var firstNameLength = 0;
var firstName = "Ada";
firstNameLength = firstName.length;
console.log(firstNameLength); // 3
var firstLetterOfFirstName = "";
var firstName = "Ada";
firstLetterOfFirstName = firstName[0]; // 0 base index
var myStr = "Jello World";
// myStr[0] = "H"; this throws an error because of the immutability of strings.
// we can change the whole content of the string variable.
myStr = "Hello World";
// not only first character we can access character at 2nd and 3rd position.
var firstName = "Ada";
var secondLetterOfFirstName = firstName[1];
var thirdLetterOfFirstName = firstName[2];
var name = "Harry";
var lastCharacter = name[name.length-1];
var name = "Ada";
var thirdToLastLetterOfName = name[name.length-3];
function wordBlanks(myNoun, myAdjective, myVerb, myAdverd){
// your code below this line
var result = "";
result += "The " + myAdjective + " " + myNoun + " " + myVerb + " to the store " + myAdverb;
// your code above this line
return result;
}
console.log(wordBlanks("dog", "big", "ran", "quickly"));
example ↓
var ourArray = ["John", 23];
var ourArray = [["the universe", 42], ["everything", 101010]];
var ourArray = [50, 60, 70];
var ourData = ourArray[0] // equals 50
var ourArray = [18, 64, 99];
ourArray[1] = 45; // ourArray now equals [18, 45, 99].
var myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], [13, 14]]];
var ourArray = ["Stimpson", "J", "cat"];
ourArray.push(["happy", "joy"]);
// ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
var ourArray = [1, 2, 3];
var removedFromOurArray = ourArray.pop();
// removedFromOurArray now equals 3, and ourArray now equals [1, 2]
var ourArray - ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals "Stimpson", and ourArray now equals ["J", ["cat"]]
var ourArray - ["Stimpson", "J", ["cat"]];
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "J", ["cat"]]
var myList = [["cereal", 3], ["milk", 2], ["bananas", 3], ["juice", 2], ["eggs", 12]];
// Funtion setup
function ourReusableFunction() {
console.log("Heyya, World");
}
ourReusableFunction(); // function call
ourReusableFunction();
// Example
function ourFunctionWithArgs(a, b){
console.log(a - b);
}
ourFunctionWithArgs(10, 5); // output 5
// Declare global variable here
var myGlobal = 10;
function fun1()
{
oopsGlobal = 5; // we can also declare variable without using "var" keyword
// if we do use "var" keyword it will be scoped to this function but we can't be used outside this function.
// but if we remove "var" keyword then the variable will automatically become global and can be accessed anywhere.
}
function func2()
{
var output = "";
if(typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if(typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
func1();
func2();
function myLocalScope() {
var myVar = 5;
console.log(myVar);
}
myLocalScope();
console.log(myVar); // this gives error
var outerWear = "T-shirt"; // global variable
function myOutfit() {
var outWear = "sweater"; // local variable
retrun outerWear;
}
console.log(myOutfit()); // output: "sweater "
console.log(outerWear); // output: "T-shirt "
function minusSeven(num) {
return num - 7;
}
console.log(minusSeven(10)); // output: 3
// Example
var sum = 0;
function addThree() {
sum = sum + 3;
}
function addFive() {
sum += 5;
}
console.log(addFive()); // undefined
var changed = 0;
function change(num) {
return (num + 5) / 3;
}
changed = change(50);
function nextInLine(arr, item) {
arr.push(item);
return arr.shift();
}
var testArr = [1, 2, 3, 4, 5];
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
function welcomeToBooleans() {
return true;
}
function ourTrueOrFalse(isItTrue) {
if(isItTrue) {
return "Yes, it's true";
}
return "No, it's false";
}
function testEqual(val) {
if (val == 12) {
return "Equal";
}
return "Not Equal";
}
testEqual(10); // returns "Not Equal"
3 === 3 // true
3 === '3' // false
function testStrict(val) {
if(val === 7) {
return "Equal";
}
return "Not Equal";
}
testStrict(10); // returns "Not Equal"
function compareEquality(a, b) {
if (a == b) {
return "Equal";
}
return "Not Equal";
}
console.log(compareEquality(10, "10")); // logs "Not Equal"
function compareEquality(a, b) {
if (a === b) {
return "Equal";
}
return "Not Equal";
}
console.log(compareEquality(10, 10)); // logs "Equal"
function testNotEqual(val) {
if (val != 99) {
return "Not Equal";
}
return "Equal";
}
console.log(testNotEqual(10)); // prints "Not Equal"
function testStrictNotEqual(val) {
if(val !== 17) {
return "Not Equal";
}
return "Equal";
}
console.log(testStrictNotEqual(10)); // prints "Not Equal"
function testGreaterThan(val) {
if (val > 100) {
return "Over 100";
}
if (val > 10) {
return "Over 10";
}
return "10 or Under";
}
console.log(testGreaterThan(10)); // prints "10 or Under"