DEV Community

Saraswathi P
Saraswathi P

Posted on

JAVASCRIPT

What is javascript?

  • javascript is the world most papular programing language and it is easy to learn.

  • javascript is used to create interactive and dynamic web pages.

  • it is responsible for adding functionality to a website and allows for user interaction with the website's content.

VARIABLES:-

variales are containers for stroing information or data.
example

var a=10
var b=20
console.log(a+b)

output:20
Enter fullscreen mode Exit fullscreen mode

in the above program a and b are variables,10 and 20 are integers.

console.log - it is the function in javascript used to print messages or values to the browser's console mainly for debugging purpose.

VARIABLE DECLARATION:-

in javascript variable can be declared in 3 ways.
1.var
2.let
3.const

using var keyword- function scope or global scope.

var x=10
{
var y=20
}
console.log(y)

output:20 //accessible inside the block.
Enter fullscreen mode Exit fullscreen mode

using let ketword- block scope.

  • the let keyword introduced in 2015.

  • the keyword must be declared before use.

  • let keyword can not be redeclared.

  • let keyword have block scope.

{
let a=10
}
console.log(a)

output
Error: a is not defined // a can't be used here becauce a is the block scope.
{
let a=10
console.log(a)
}

ouput:20 // a can be used hear.

Enter fullscreen mode Exit fullscreen mode

using const keyword- block scope.

  • in javascript the const keyword is used to declare a constant variable.

  • a constant variable once assinged value cannot be reassinged or redeclared.

  • it provide a way to create variable that are meant to be immutable.

const a=10
a=20
console.log(a)

output
Error:assingnment to constant variable. //const cannot be reassinged 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)