JavaScript Data Types
JavaScript is a single-threaded language because while running code on a single thread, it can be really easy to implement as we don't have to deal with the complicated scenarios that arise in the multi-threaded environment like a deadlock. Since JavaScript is a single-threaded language, it is synchronous in nature
What does mean by declaration- assignment- initialization !! in java script ?? Initialization: When you declare a variable it is automatically initialized, which means memory is allocated for the variable by the JavaScript engine.
Assignment: This is when a specific value is assigned to the variable.
And Hoisting available only for declaration , its not available for assignment and initialization.
// Hoisting in JavaScript
they are ..
var x = 25; // Number
var x = true ; // Boolean
Different types of variable value is a data types, they are..
JavaScript Strings
A string (or a text string) is a series of characters like "John Doe". Strings are written with quotes. You can use single or double quotes:
let carName1 = "Volvo XC60"; // Using double quotes
let carName2 = 'Volvo XC60'; // Using single quotes
JavaScript Numbers
JavaScript has only one type of numbers.
Numbers can be written with, or without decimals:
let x1 = 34.00; // Written with decimals
let x2 = 34; // Written without decimals
document.write(x1);
JavaScript Arrays
JavaScript arrays are written with square brackets. Array items are separated by commas. The following code declares (creates) an array called cars
, containing three items (car names):
array with index
var ary = [10,20,30,40,50];document.write(ary[2]);
array with for loop..
var ary = [10,20,30,40,50];for (var a = 0; a <= 4; a++){ document.write(ary[a]+"<br>"); }
nested array
<script>
var user =["ramesh", 22, "pune", ["Male",25000]];
var [name, age = 20, city , [gender, salary]] = user;
console.log(name); console.log(age); console.log(city); console.log(gender); console.log(salary);
</script>
array with function..
<script>
function user([name,age=20, city]){ console.log(name); console.log(age); console.log(city); }
user(["code guruva",22, "Delhi"]);
</script>
JavaScript Objects
JavaScript objects are written with curly braces {}
.
Object properties are written as name:value pairs, separated by commas.
Object Literals
Undefined
In JavaScript, a variable without a value, has the value undefined
. The type is also undefined
. Any variable can be emptied, by setting the value to undefined
. The type will also be undefined
.
let car; // Value is undefined, type is undefined
car = undefined; // Value is undefined, type is undefined
Empty Values
An empty value has nothing to do with undefined
.An empty string has both a legal value and a type.
let car = ""; // The value is "", the typeof is "string"
Ex :
Null Value :
JavaScript Booleans
Booleans can only have two values: true
or false
. Booleans are often used in conditional testing.
let x = 5;
let y = 5;
let z = 6;
(x == y) // Returns true
(x == z) // Returns false
Ex :