Variable and Datatypes

Variable and Datatypes

Variable:

  1. The variable is used to store some info or data or some value we can store our value or data in three different types.
  • var: var is also used to declare a variable but it's good not to use var

  • let: with the help of let also we can declare a variable and let allows you to change its value even if it is declared.

  • const: const is something that doesn't allow you to change its value once it is declared.

  1. We can declare a variable with the help of var, let, and const (followed by a variable name) but we should avoid using var. it's better to use let.

let firstName; this is called declaring a variable

let firstName = "Deepak" this is initializing value to it now Deepak will store in firstName.

  1. The variable is case-sensitive. This means username and Username are not the same if we are declaring something with username and assigning a value to it then during printing its value we have to write console.log(username) we cannot write console.log(userName)
let username = "deepak kumar nayak"
console.log(username)--- this will give us the correct output.
console.log(userName)--- this will give us a Reference Error i.e userName is not defined.
  1. Variable name contains an underscore and dollar sign we cannot use any other symbol during naming a variable, space is also not allowed to declare a variable
let user_name = "deepak" --- this is allowed.
let user$name = "deepak" --- this is allowed.
let username1 = "deepak" --- this is also allowed.
let user name = "deepak" --- this is not allowed.
let 1username = "deepak" --- this is not allowed.

Note: for a good naming convention is better not to use numbers during variable declaration. we can use camelcase for example:

let userName = "deepak"

After declaring a variable we can initialize a value to it and we can also change its value later. We can initialize a value by using the assignment operator (=)

let companyName = "Ineuron"
companyName = "TCS"  
console.log(companyName)
Output: TCS

if we are only declaring a variable and not assigning a value to it then that particular variable is undefined for example:

let companyName:
console.log(companyName)

if we want to access an undeclared variable then we will get an error as Reference Error that the variable is not defined because it is an undeclared variable we haven't declared it.

console.log(cityName)
Reference Error cityName is not defined.

Datatypes:

Javascript has primitive Datatypes and non-primitive Datatypes.

  1. Primitive Datatype

  2. Non-Primitive Datatype

Primitive Datatype:

  • number

  • string

  • boolean

  • undefined

  • symbol

  • bigInt

let userName = "Deepak" // this is a string type
let EmployeeId = 121  // this is an number type 
let temperature = 40.0 // this is also a number type
let is_married = false // this is a boolean type
let companyName; // now it is undefined

//Note: when we are declaring a variable but we haven't assigned a value to it by default undefined will be stored inside it 

console.log(companyName)                // undefined
console.log(typeof companyName))        // undefined
as soon as we assign value to it we will not get undefined because that particular value will be stored which we have assigned to it.
  1. Number Datatype: Javascript represents both Integer and float numbers as a number type. it automatically converts the floating point number into an integer.

  2. String Datatype: A string is a sequence of characters we can use a double quote(" ") as well as a single quote to declare a string value.

let username = "deepak" 
let username = 'deepak'
// string indexing

let username = "deepak"
console.log(username[0])
console.log(username[1])
console.log(username[2])
console.log(username[3])
console.log(username[4])
console.log(username[5])

Output
-------
d
e
e
p
a
k

// Accessing the length of the string.
let username = "deepak"
console.log(username.length)

Output
------ 
6

// Note: The Index always starts from zero but if we want to access the length of the string then it will start from 1

let username = "deepak"
console.log(username[username.length-1])

Output
------
"k"

Some String Methods :

let username = "deepak"
console.log(username.toLowerCase())
console.log(username.toUpperCase())
console.log(username.indexOf("d"))
console.log(username.concat(' kumar'))

console.log(username.endsWith("k"))
console.log(username.startsWith('d'))
console.log(username.endsWith("r"))
console.log(username.startsWith("p"))


console.log(username.includes("l"))

console.log(username.repeat(2))

let fullName = "deepak kumar nayak"
console.log(fullName.split(" "))
console.log(fullName.slice(0,10))

let surname = "   nayak    "
console.log(surname.trim())

Output
-------
deepak
DEEPAK
0
deepak kumar
true
true
false
false
false
deepakdeepak
[ 'deepak', 'kumar', 'nayak' ]
deepak kum
nayak

// converting an integer into a string: 
let age = 22
age = String(age)
console.log(typeof age)

Output: string

// string concatenation
let nameOne = "deepak"
let nameTwo = "Kumar"

console.log(nameOne + nameTwo)

Output: deepakKumar

// Template String 

let name = "Deepak Kumar Nayak"
let batch = "BatchTwo"
let courseName = "Fullstack Javascript Bootcamp-2"

let aboutme = `My name is ${name} from ${batch} 
and my coursename is ${courseName}`
console.log(aboutme)

Output:
---------
My name is Deepak Kumar Nayak from BatchTwo
and my coursename is Fullstack Javascript Bootcamp-2
  1. Boolean Datatype: The Boolean Datatype has two values true and false true means 1 and false means 0.
  1.  let isLoogin = true;
     let iscourseCompleted = false;
    

Non-Primitive Datatype:

Array and Object both are called Non-Primitive Datatype.

  • Array:- The Array is something where we can store multiple values of different data types, we will study more about an array in a different blog now just look at an example of an array.

    arrayOne = [1, 2, 3, 'Deepak', true, 9.0, ]

  • Object: Objects are being created using double curly braces with a key-value pair.

      let EmployeeOneData = {
        'firstName': 'Deepak Kumar',
        'lastName': 'Nayak',
        'email': 'deepaknnayak34@gmail.com',
        'Location': 'Kolkotta',
        'phone': 9090876780,
      }
    
      console.log(EmployeeOneData['phone'])
      console.log(EmployeeOneData.phone)
    

I have created an Object and I am accessing the properties of an object by using dot (.) or by using a square bracket ['keyname']

Note: we will discuss Array and Object in a different blog.