DEV Community

Cover image for Javascript DataTypes
pavanbaddi
pavanbaddi

Posted on • Originally published at thecodelearners.com

Javascript DataTypes

In this tutorial, You'll be learning about using datatypes in Javascript.

Example


let x = 16; //now x is interger
x = 12.65; //now x value is updated to float
x = "JavaScript is Amazing."; //now x value is updated to string
document.write(x);

//output
JavaScript is Amazing.

DataType types

String

Strings are a sequence of characters wrapped in double or single quotes.
Below ways, we can assign Strings.

let task_one = "Complete Homework<br>";
//OR 
let task_two = 'Complete Homework<br>';
//OR
let task_three = `Complete Homework<br>`; //this is called string literals

document.write(task_one);
document.write(task_two);
document.write(task_three);

//output
Complete Homework
Complete Homework
Complete Homework

String Concatenation

Using + Operator

let name = "John";
let age = 25;
let sentence = name+" is "+age+" years old";
document.write(sentence);

//output
John is 25 years old

Using String Literals


let name = "John";
let age = 25;
let sentence = `${name} is ${age} years old`;
document.write(sentence);

//output
John is 25 years old

Numbers

Adding two numbers

let a=5,b=7;
let z=a+b;
alert(z);

Add number and string

let a=5;
let b="7";
let z=a+b;
alert(z); //output 57

null type

If we don't want to initialize the value to variable than we can use null which basically means nothing.

let designation=null
alert(designation); // outputs null

undefined type

If we declare variable but don't initialize its value than the value of the variable will be undefined.

let name;
alert(name); //outputs undefined

Boolean Date-Type

Boolean types have two values that are true or false. We can also assign 1 which means true and 0 which means false.

let has_completed_howework=true;
alert(has_completed_howework); //outputs true

We can use this for conditional checking

let has_completed_howework=true;
if(has_completed_howework==true){
    alert("Student has completed homework");
}else{
    alert("Student has not completed homework");
}

I’ve included link to the whole chapter here.

Top comments (0)