Basics of JavaScript

Author: neptune | 15th-Apr-2022
#JavaScript


Variables in JS

Variables are containers that store values. Variable names can be any legal identifier.

You can start by declaring a variable with the var (less recommended) or let keyword, followed by the name you give to the variable.

Important point to be remembered about Variables.

Important points to remember about variables.

  • var is the keyword to declare a variable.

  • Variables are the identifiers to store data values.

  • ‘=’ is used to assign a value to a variable.

  • Variable names must start with _ or $ or letter.

Example:

 99temp is invalid, whereas _99temp is valid.

What is ECMAscript ?

ES2015(known as ECMAscript), two other ways to declare variables were introduced- let and const. We can use these 2 keywords to define variables(now recommended).

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.





Operators in JS

Just like the other languages JavaScript have the following operators.

We are going to explore these operators.

  • Assignment operators

  • Arithmetic operators

  • Comparison operators

  • Logical operators

  • String operators

  • Conditional operators

1. Assignment Operators “=”

  • Variables can be declared either by key var and value OR simply by assigning values directly. 

Example : 

 var x = 42; 

 OR 

 x = 42 ;  


  • We can assign the values of different data types to the same variable.

Example: 

 var x = 34; 

 var x = “Neptune”; 


  • Type of a variable can be checked by using typeof operator

Example: 

 var x = "Neptune"; 

 console.log(typeof x); //returns String 

2.Comparison Operators

  • JavaScript has operators like <, >, !=, >=, <= to compare 2 operands.

  • What is the difference between == and === operators ?

    • "==" compares the operands and returns true without considering their data type.
    • Example:
    • var a = 10, b= “10”;
    • (a==b) results in true due to the same value they carry, but ignores data types differentiation.
    • However, (a===b) results in false as they are of different data types.






3.Standard Arithmetic operators

  • Addition + Ex: [5 + 8]

  • Subtraction - Ex: [49 – 38]

  • Division / Ex: [ 49 / 7]

  • Multiplication * Ex: [28 * 2]

  • Modulus % to return the remainder of a division – Ex: 50 % 7 is 1

  • Increment ++ to increment the operand itself by 1 – Ex: If x=4, x++ evaluates to 5

  • Decrement -- to decrement the operand itself by 1 – Ex: if x= 10, x—- evaluates to 9

4.Logical Operators

AND &&, OR ||, NOT ! are the logical operators often used during conditional statements to test logic between variables.

  • Expr1 && Expr2 returns true if both are true, else returns false.

  • Expr1 || Expr2 returns true if either is true.

  • !Expr1 operates on a single operand to convert true to false and vice versa.

5.String Operator

Operator "+" is used to concatenate strings.

 var x = "Hello"; 

 var y = " World "; 

 console.log(x + y); 

/* Returns “Hello World” */

More on String Operator

While concatenating, JavaScript treats all data types as strings even if values are of different data types.

 var x = "Hello"; 

 var y = 100; 

 var z = 333; 

 console.log(x + y + z); 

/*  Returns “Hello100333” */





anonymous | June 2, 2021, 11 a.m.

Also add conditional and events



Related Blogs
Arrow Functions in JavaScript | ES6
Author: neptune | 26th-Mar-2023
#JavaScript #React.js
In this article, we will explore the syntax and usage of arrow functions in detail, along with some examples...

Different ways to handle state in React applications
Author: neptune | 21st-Jun-2023
#JavaScript #React.js
This article explores different ways to manage states in React, including local component state, context API, and state management libraries like Redux...

Counter | #2620 | LeetCode Solution
Author: neptune | 02nd-Sep-2023
#JavaScript #LeetCode
Given an integer n, return a counter function. This counter function returns n and then n + 1, n + 2, etc...

To Be Or Not To Be | #2704 | LeetCode Solution
Author: neptune | 03rd-Sep-2023
#JavaScript #LeetCode
Write a function that helps developers test their code. It should take in any value and return an object with the following two functions...

Apply Transform Over Each Element in Array | #2635 | LeetCode Solution
Author: neptune | 05th-Sep-2023
#JavaScript #LeetCode
Given an integer array `arr` and a mapping function `fn`, return a new array with a transformation applied to each element...

Is Object Empty | #2727 | LeetCode | JavaScript Solution
Author: neptune | 01st-Sep-2023
#JavaScript #LeetCode
Given an object or an array, return if it is empty...

Counter 2 | #2665 | LeetCode Solution
Author: neptune | 04th-Sep-2023
#JavaScript #LeetCode
Write function 'createCounter' It accept an initial integer 'init' It should return an object with three functions- increment() , decrement(), reset()...

Filter Elements from Array | #2634 | LeetCode Solution
Author: neptune | 06th-Sep-2023
#JavaScript #LeetCode
Given an integer array `arr` and a filtering function `fn`, return a filtered array `filteredArr`...

Function Composition | #2629 | LeetCode Solution
Author: neptune | 09th-Sep-2023
#JavaScript #LeetCode
Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions...

Managing Virtual Environments in React JavaScript Projects
Author: neptune | 28th-Jun-2023
#JavaScript #React.js
Virtual environments are a valuable tool in React JavaScript projects as they allow developers to isolate dependencies, manage package versions, and maintain project consistency...

All You Need to Know About Pure Functions & Impure Functions in JavaScript
Author: neptune | 02nd-Apr-2023
#JavaScript #React.js
You should try to use pure functions whenever possible and avoid using impure functions unless necessary...

Add Two Promises | #2723 | LeetCode Solution
Author: neptune | 12th-Sep-2023
#JavaScript #LeetCode
Given two promises `promise1` and `promise2`, return a new `promise`. `promise1` and `promise2` will both resolve with a number...

Array Reduce Transformation | #2626 | LeetCode Solution
Author: neptune | 09th-Sep-2023
#JavaScript #LeetCode
Given an integer array `nums` and a reducer function `fn`, and an initial value `init`, return a reduced array...

Memoize | #2634 | LeetCode Solution
Author: neptune | 12th-Sep-2023
#JavaScript #LeetCode
A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value...

Chunk Array | #2677 | LeetCode Solution
Author: neptune | 19th-Sep-2023
#JavaScript #LeetCode
Given an array arr and a chunk `size`, return a `chunked` array...

Array Prototype Last | #2619 | LeetCode Solution
Author: neptune | 20th-Sep-2023
#JavaScript #LeetCode
Write code that enhances all arrays such that you can call the `array.last()` method on any array and it will return the last element...

Allow One Function Call | #2666 | LeetCode Solution
Author: neptune | 11th-Sep-2023
#JavaScript #LeetCode
Given a function `fn`, return a new function that is identical to the original function except that it ensures `fn` is called at most once...

View More