blog bg

October 02, 2023

JavaScript Fundamentals - Summary with Examples

Share what you learn in this blog to prepare for your interview, create your forever-free profile now, and explore how to monetize your valuable knowledge.

Write JavaScript code inside Html File?

<script type="text/javascript"> 
  // your javaScript code
</script>
Import an external JavaScript within a Html File?
<script src="path/to/file"></script>
 
VARIABLES

 var :
var x = 1;
if (x === 1) {
    x = 2;
    console.log(x); // Output: 2
}
let:
let x1 = 1;
console.log(x1); // Output: 1
if (x1 === 1) {
    x1 = 2;
}
console.log(x1); // Output: 2
const:
const number = 48;
console.log(number); //48
number = 55 // it will throw an error
 
 
DATATYPES
 
Number
let x = 25
String
let string = "JavaScript is fun"
Boolean
let isValid = true/false
Null
let money = null
Undefined
let year;
let currYear = undefined
Object
const obj = {
  key1: "value1",
  key2: "value2",
}
Array
const arr = ['el1','el2','el3'];
 
 
OPERATORS
 
Arithmetic(+,-,*,/,%,++)
let x = 5;
let y = 3;

// Addition
console.log("x + y = ", x + y); // 8
// Subtraction
console.log("x - y = ", x - y); // 2
// Multiplication
console.log("x * y = ", x * y); // 15
// Division
console.log("x / y = ", x / y); //1.6
// Remainder
console.log("x % y = ", (x % y)); // 2
// Increment
console.log("++x = ", ++x); // x is now 6
console.log("x++ = ", x++);
console.log("x = ", x); // 7
Comparison(==, ===,!=,>,<,>=,<=)
// Comparison
console.log(x > y); // true
// Equal operator
console.log((2 == 2)); // true
// Not equal operator
console.log((3 != 2)); // true
// Strict equal operator
console.log((2 === 2)); // true
// Strict not equal operator
console.log((2 !== 2)); // true
Logical(A&&B, A ||B, !A)
// Logical AND
console.log((x < 6 && y < 5)); // true
// Logical OR
console.log((x < 6 || y > 6)); // true
// Logical NOT
console.log(!(x < 6)); // false
Assignment(=, +=,-=,*=,/=,%=)
let x = 5;
let y = 3;

console.log(x);//5
x += 10
console.log(x);//15
x -= y
console.log(x);//12
x *= y
console.log(x);//36
x /= y
console.log(x);//12
 
 
ARRAYS
const numbers = [10,13,16];
//Add Element at the end of the array
numbers.push(19);
console.log(numbers);//[10, 13, 16, 19]
//Add element at the beginning of the array
numbers.unshift(8);
console.log(numbers);//[8, 10, 13, 16]
//remove the last element
numbers.pop();
console.log(numbers);//[10, 13]
//remove the first element
numbers.shift();
console.log(numbers);//[13,16]
//find the position of specific element
const index = numbers.indexOf(13);
console.log(index);//1
//check if the array has the the specific element
console.log(numbers.includes(13));//true
console.log(numbers.includes(15));//false
//remove the specific element
numbers.splice(1,1);
console.log(numbers);//[10,16]
 
OBJECTS
const person = {   
 name:"john",    
 age:41,    
 job:'retailer',    
 hobbies:['music','movies','golf'],    
 married:true,    
 birthYear:function(){        
 		this.bornYear = 2023 - this.age;
 		return this.bornYear
 	} 
 } 
 person.birthYear(); 
 console.log(person.bornYear);//1982
 
 
LOOPS
 
for
for(let x = 0; x < 4; x++){
    console.log("Value of x:" + x);
}
Output:

Value of x:0
Value of x:1
Value of x:2
Value of x:3
while
let y = 1;
  
while (y <= 4) {
    console.log("Value of y:" + y);
    y++;
}
Output:

Value of y:1
Value of y:2
Value of y:3
Value of y:4
do-while
let x = 0;
do {
    console.log("Value of x:" + x);
    x++;
} while (x < 4);
Output:

Value of x:0
Value of x:1
Value of x:2
Value of x:3
 
 
STATEMENTS
 
if-else
const i = 10;
if (i < 15)
    console.log("Value of i is less than 10");
else
    console.log("Value of i is greater than 10");

Output:

Value of i is less than 10
switch
let day = "monday"

switch (day) {
  case 'monday':
    console.log("today is monday");
    break;
  case 'tuesday':
    console.log("today is tuesday");
    break;
  case 'wednesday':
    console.log("today is wednesday");
    break;
  default:
    console.log("invalid day");
}
Output:
today is monday

 

EXERCISES

I have attached the github link with examples and answers. I hope it will help you to understand the Javascript Fundamentals concept further.

Next ArticleJavaScript: Modern Operators

146 views

Please Login to create a Question