Javascript Variables and operators
Ramya K

Ramya K @ramya_kamalasekaran

About: Having 2.5 years of Experience in Software field. After a career gap planning to start my career again

Joined:
Jan 26, 2025

Javascript Variables and operators

Publish Date: Jun 20
1 0

Variables:
It is a container to store values or it is named memory location.
it is also called as identifier or reference variable.
It varies value so it is called variable.
a=10;

Rules for creating variable name:
1.Each variable has unique name and case sensitive.
2.It can start with letter, $ and _ and not with number.

  1. It should not use reserved words.

Naming convention:
camel case- variable name and function name follows- employeeId
Pascal case- class and Interfaces follows- Employee
under case- employee_Id

keywords which are used to declare variables:

  • let _– when we assign the value to the variablei.e during declaration we give let a=10; re declaration is not allowed. Reassignment a=15 is allowed *_const _– re-declartion and reassignment both not allowed *_var – it is legacy keyword not recommended to use. It allows both re declaration and reassignment. var a=10; var a=15;

To see the output of the js ie. console use(ctrl+shift+i) or right click on the browser webpage and inspect

if we reassign or redeclare the const, we will get error
Printing multiple variable by comma separation:
let a=10;
let b=15;
we can print more that one variable by comma separation
console.log(a,b);

printing statements:

  1. alert(“hi”) or we can pass variable alert(a) – one button- ok
  2. confirm(“hi”) – same as alert but in confirm there will be 2 buttons- ok and cancel
  3. document.write() or writeln- prints in UI
  4. prompt(“enter the num”)- used to get input dynamically from user let num=prompt(“enter the num”) console.log(num)

different methods in console:
let a;
1.console.log(a)- undefined – variable is declared but initialized means we get undefined value.
2.console.error(a)-displays undefined as error
3.console.warn(a)-displays undefined as warning
4.console.clear()- clears all the printed msg

Operator:
It is used to perform operations.

Types of Operators:
1.Arithmetic operator
2.Comparison operator or Relational operator
3.Assignment operator
4.Logical operator
5.Bitwise operator
6.Ternary operator
7.unary operator

1.Arithmetic operator:
It is used to perform arithmetic operations.Operator:
It is used to perform operations.

  • (addition)
  • (subtraction)
  • (multiplication) / (division) % (modulus - remainder of division) ** (exponentiation - ES2016) ++ (increment) -- (decrement)
  1. Assignment operator:
    Assign values to variables.

    = (assignment)
    += (addition assignment)
    -= (subtraction assignment)
    = (multiplication assignment)
    /= (division assignment)
    %= (modulus assignment)
    *
    = (exponentiation assignment)
    assigning right hand value to left hand variable.
    += -= = /= %= *=
    assigning value to the same variable.
    a=a+1
    a+=1, a-=1

3.Comparison or relational operator:
compares 2 values and returns boolean value(true or false)
== (equality)
!= (inequality)
=== (strict equality - checks value and type)
!== (strict inequality)

(greater than)
< (less than)
= (greater than or equal to)
<= (less than or equal to)

4.Logical operator:
Combine or manipulate Boolean expressions.

&& (logical AND)
|| (logical OR)
! (logical NOT)
Enter fullscreen mode Exit fullscreen mode

Concatenation (+)
Combining more than one string is called concatenation.
This is just like java concatenation.

Example:
let num=5;
let first=1;
console.log(num+”*”+first”=”num*first)

To ease the string concatenation template string is introduced since ES6 version.

Template string or Template literal:

Here we use `` backtick and to get the value of the variable inside backtick use ${}.

Comments 0 total

    Add comment