var arr_data = [1,2,0,4,6,7,3,4,2,5,6,4,3,8,9]; var arr_lgh = arr_data.length; //console.log(arr_lgh); //print array element for(i=0; i<arr_lgh;i++){ //console.log(arr_data[i]); } //find max and min value element using sort() method assending order var arr_data = [1,2,0,4,6,7,3,4,2,5,6,4,3,8,9]; var arr_sort_asc = arr_data.sort(function(a,b){return a-b}); //console.log(arr_sort_asc); var max = arr_sort_asc[arr_lgh-1]; //console.log(max); var min = arr_sort_asc[0]; //console.log(min); //search array element var data = [20,2,98,3,6,44,25,12,45,34,67,8]; var serch_ele = 67; var data_length = data.length; for(var i=0; i < data_length; i++){ if(data[i] == serch_ele){ var position = i; var element = data[i]; } } //console.log("the element is : "+element); //console.log("the position of given element is : "+position); //search multiple array elements at a time var data = [20,2,98,3,6,44,25,12,45,34,67,8]; var serch_ele1 = 45; var serch_ele2 = 25; var serch_ele3 = 20; var data_length = data.length; for(var i=0; i < data_length; i++){ if(data[i] == (serch_ele1)){ var position = i; var element = data[i]; } if(data[i] == (serch_ele2)){ var position2 = i; var element2 = data[i]; } if(data[i] == (serch_ele3)){ var position3 = i; var element3 = data[i]; } } //console.log("the element is : "+element); //console.log("the position of given element is : "+position); //console.log("the element is : "+element2); //console.log("the position of given element is : "+position2); //console.log("the element is : "+element3); //console.log("the position of given element is : "+position3); var data = [20,2,98,3,6,44,25,12,45,34,67,8]; var data_length = data.length; for(var i=0; i < data_length; i++){ if(data[i] == 25){ //console.log("the position is : "+i); } } var data = [20,2,98,3,20,44,25,12,45,20,67,8,20]; var data_length = data.length; var given_ele = 20; //console.log("the position of given element in an array : "); for(var i=0; i < data_length; i++){ if(data[i] == given_ele){ // console.log(i); } } //reverse an array for(var i=data_length-1; i>=0; i--){ //console.log(data[i]); } //assigning array in given array var data = [20,2,98,3,20,44,25,12,45,20,67,8,20]; var data_length = data.length; var element = 200; for(var i=0; i<data_length; i++){ if(data[i]==20) data[i] = 200; } //console.log(data); //find min and max element (max) var datas = [20,2,9,3,6,44,25,12,45,34,3,60,23,40,80]; var datas_lengths = datas.length; var max = datas[0]; for(var i=0; i < datas_lengths; i++){ if(max < datas[i+1]){ max = datas[i+1]; datas[i+1] = max ; } } //console.log("the max is: "+max); //find min element var datas = [20,2,9,3,6,44,25,12,45,34,3,60,23,40,80,1,0]; var datas_lengths = datas.length; var min = datas[0]; for(var i=0; i < datas_lengths; i++){ if(min > datas[i+1]){ min = datas[i+1]; datas[i+1] = min ; } } //console.log("the min is: "+min); var data = ['shubham','jaiswal','kumar','kumar','password','kumar']; datalg = data.length; for(i=0; i<datalg; i++){ if(data[i]=="kumar"){ data[i] = "papa"; } } //console.log(data); //more javascript array practice
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");