Basics of JavaScript

Author: neptune | 15th-Apr-2022 | Views: 676
#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
Introduction to React
Author: neptune | 11th-Dec-2022 | Views: 521
#JavaScript
Virtual DOM is an lightweight copy of DOM. It can be changed when we want and then can be saved to the real DOM. Every change Virtual DOM efficiently rerenders the DOM...

Java Script Interview Questions
Author: neptune | 09th-May-2022 | Views: 316
#JavaScript #Interview
Basic JavaScript questions asked in interviews : 1. What are the different data types present in javascript? 2. Difference between "==" and "===" operators...

What is React.js?
Author: neptune | 26th-Nov-2022 | Views: 274
#JavaScript
Imagine a post on Facebook. Now this post which is a container consists of multiple small parts like likes, comments, share, comment box called components...

Top 10 Reasons to learn React
Author: neptune | 24th-Nov-2022 | Views: 273
#JavaScript
React is a popular open-source JavaScript library to create robust and dynamic user interfaces for web applications...

View More