Learn JavaScript Programming with examples.

Bittu Kumar
3 min readJun 11, 2021

--

Here are the topics to cover in basics of javascript.

  1. Comments in javascript
    // inline comment

/*
some comments in multi-line
line 1
line 2
*/

2. Data types in javascript
Javascript provides eight different types of data types.
number, string, boolean, object, null, undefined, symbol, bigint.
In javascript variable names can be of numbers, letters, and $ or _, but may not contain spaces or start with a number
var a=5;

3. when you need string with quotes inside string.

just put backslash with single quote or backslash with double quote.
ex- var sampleStr = "Alan said, \"Peter is learning JavaScript\".";
ex-2
var str2= ‘i am bittu, hey let\’s go somewhere’;

4. find string length by .length property.
var first=”myname”
console.log(first.length)
//6

5. you can find nth character in string by using bracket notation.
first character in string in always at zeroth position. (0th).
var firstName = "Ada";
var secondLetterOfFirstName = firstName[1];

6. JavaScript Array toString()
arr.toString() →
*toString() method does not have any parameters.
*It returns a string, represent the value of array separated by comma.
*var info = ["car", 28, "delhi"];

var info_str = info.toString();

// toString() does not change the original array
console.log(info); // [ 'car', 28, 'delhi' ]

// toString() returns the string representation
console.log(info_str); // car,28,delhi

var collection = [5, null, 10, "JavaScript", NaN, [3, [14, 15]]];
console.log(collection.toString()); // 5,,10,JavaScript,NaN,3,14,15

7. Javascript Array pop()
* Remove last element of array and return that element.
* This method changes its original array and its length.
var car = ["baleno", "indica", "swift", "alto", "etios"];

var popped = car.pop();

console.log(car); // [ "baleno", "indica", "swift", "alto" ]
console.log(popped); // "etios"

// pop returns any type of object
var numbers = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90],
];
console.log(numbers.pop()); // [ 70,80,90]
console.log(numbers); // [ [ 10, 20, 30 ], [ 40, 50, 60 ] ]

8. Javascript Array shift()
* shift() method in array removes the first element from array and return that element.
* changes original array and its length.
var car = ["baleno", "indica", "swift", "alto", "etios"];

var shifted = car.shift();

console.log(car); // [ "indica", "swift", "alto", "etios"]
console.log(shifted); // "baleno"

// shift returns any type of object
var numbers = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90],
];
console.log(numbers.shift()); // [ 10,20,30]
console.log(numbers); // [ [ 40, 50, 60 ], [ 70, 80, 90 ] ]

https://carbon.now.sh/4bTrOOFFvdzpXniS6kp9

8. Coercion in javascript
in order to compare two value of two different datatypes, (for ex- number, string etc) it must convert one to another to compare, this is called Type coercion. Once it does, it can compare as follows,

1 == 1 // true
1 == 2 // false
1 == '1' //true
"3" == 3 //true
1 != 2 // true
1 != "1" //false
1 != '1' //false
1 != true //false
0 != false //false
3 ! == 3 // false
3 ! == ‘3’ //true
4 ! == 3 //true

9. Objects in Javascript
basic js object example:
let duck = {
name: "Aflac",
numLegs: 2
};
dot notation is used in order to get the value of any property of objects.
ex- console.log(duck.name); // Aflac
objects have special type of property called method.
methods are property which is function. means function are defined inside the objects. this way behaviour of the objects is completely changed.

Below is the example.

let duck = {
name: "Aflac",
numLegs: 2,
sayName: function() {return "The name of this duck is " + duck.name + ".";}
};
duck.sayName();
  • **Javascript is single threaded and single stack, so that is why asynchronous handling becomes very important.
    ***Execution context is an environment in which code is run.

10. Difference between let and var keyword
* declaration of variable with var, that you can over write the variable declaration without any error.
example-
var name= “bittu”;
var name= “amit”;
console.log(name); // amit

this will become great problem over the time. to over come this problem, ES6 introduce new keyword- let.
Example-
let animal= “cow”;
let animal =”dog”;
this will show error.

Will update this page regularly……………………..

let me know if you need discussion in any points.

--

--

Bittu Kumar
Bittu Kumar

Written by Bittu Kumar

Exploring Angular and Frontend development.

No responses yet