JavaScript Basics - Notes

1. comments :

 multi-line use  /***  **/  and in-line use  // 

2. JavaScript Variables :

  • Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number

  • When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means "Not a Number". If you concatenate a string with an undefined variable, you will get a literal string of "undefined".

// Only change code below this line
var a;
var b;
var c;
// Only change code above this line

a = a + 1;
b = b + 5;
c = c + " String!";
output :
The value of a is: NaN The value of b is: NaN The value of c is: undefined String!

3. Augmented Assignment

//Augmented Addition 
var a= 20;
a += 5; // a= a+ 5;
//Augmented Subtraction
a -= 5;
// Augmented Multiplication
a *= 5;

4. Escaping Literal Quotes in Strings

When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: " or ' inside of your string?

In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash () in front of the quote.

var sampleStr = "Alan said, \"Peter is learning JavaScript\".";

5. bracket notation

var strVal="kushal";

var firstCharOfString=strval[0];

6. Understand String Immutability

In JavaScript, String values are immutable, which means that they cannot be altered once created.

//For example, the following code:
//wrong
var myStr = "Bob";
myStr[0] = "J";
//possible
var myStr = "Bob";
myStr = "Job";

Array

1. Nest one Array within Another Array

Array can hold array itself also.

var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; console.log(myArray) //output : [[1, 2],[3, 4, 5], [6, 7, 8, 9]]

  • Unlike strings, the entries of arrays are mutable and can be changed freely.

  • Access Multi-Dimensional Arrays With Indexes

With Indexes One way to think of a multi-dimensional array, is as an array of arrays. When you use brackets to access your array, the first set of brackets refers to the entries in the outer-most (the first level) array, and each additional pair of brackets refers to the next level of entries inside.

  • Multidimensional arrays are not directly provided in JavaScript. If we want to use anything which acts as a multidimensional array then we need to create a multidimensional array by using another one-dimensional array. So multidimensional arrays in JavaScript is known as arrays inside another array
// Setup
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

// Only change code below this line
var myData = myArray[2][1]; // output 8
  1. push()

Append data to the end of an array is via the push() function.

var arr1 = [1,2,3];
arr1.push(4);
// arr1 is now [1,2,3,4]
  1. pop()

.pop() is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element.

var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();

console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
  1. shift()

That's where .shift() comes in. It works just like .pop(), except it removes the first element instead of the last.

var ourArray = ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].
  1. unshift() .unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array.
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "J", "cat"]

Functions

1. Global Scope and Functions

In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.

Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.

var myGlobal=10;
function fun1() {
  // Assign 5 to oopsGlobal Here
oopsGlobal=5; // **though it is under block scope, but due to missing of var - it became global variable**
}

// Only change code above this line

function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

2. Global vs. Local Scope in Functions

It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.

var someVar = "Hat";
function myFun() {
  var someVar = "Head";
  return someVar;
}

The function myFun will return "Head" because the local version of the variable is present.

3. Understanding Undefined Value returned from a Function A function can include the return statement but it does not have to. In the case that the function doesn't have a return statement, when you call it, the function processes the inner code but the returned value is undefined