किसी विशिष्ट स्थिति से मान को बदलने के लिए, जावास्क्रिप्ट में ब्याह () का उपयोग करें। निम्नलिखित कोड है -
उदाहरण
var changePosition = 2
var listOfNames = ['John', 'David', 'Mike', 'Sam','Carol']
console.log("Before replacing=");
console.log(listOfNames);
var name = 'Adam'
var result = listOfNames.splice(changePosition, 1, name)
console.log("After replacing=");
console.log(listOfNames) उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
आउटपुट
यहाँ, मेरी फ़ाइल का नाम है demo14.js। यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\JavaScript-code> node demo154.js Before replacing= [ 'John', 'David', 'Mike', 'Sam', 'Carol' ] After replacing= [ 'John', 'David', 'Adam', 'Sam', 'Carol' ]